1 16 17 package sample.amazon.search; 18 19 import javax.swing.*; 20 import javax.swing.event.HyperlinkEvent ; 21 import javax.swing.event.HyperlinkListener ; 22 import java.awt.*; 23 import java.io.IOException ; 24 25 31 class LinkFollower implements HyperlinkListener , Runnable { 32 33 36 protected static boolean showURL = false; 37 38 41 private static JFrame f; 42 43 46 private static JPanel contentPane; 47 private static JEditorPane jep; 48 private static JScrollPane scrollPane; 49 50 53 private static boolean builded = false; 54 55 58 private static String currentURL; 59 60 63 public LinkFollower() { 64 65 } 66 67 73 public void hyperlinkUpdate(HyperlinkEvent evt) { 74 if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 75 if (builded == false) { 76 77 78 79 buildURLWindow(); 80 } 81 try { 82 currentURL = evt.getURL().toString(); System.out.println("Going to " + currentURL); 84 showURL = true; } catch (Exception e) { 86 System.out.println("ERROR: Trouble fetching url"); 87 } 88 } 89 } 90 91 95 protected void setPage() { 96 jep.setEditable(false); 97 jep.addHyperlinkListener(new LinkFollower()); 98 f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 99 100 contentPane.setLayout(new BorderLayout()); 101 contentPane.setPreferredSize(new Dimension(400, 100)); 102 contentPane.add(scrollPane, BorderLayout.CENTER); 103 104 f.pack(); 105 f.setSize(640, 360); 106 f.setVisible(true); 107 try { 108 jep.setPage(currentURL); 109 } catch (IOException e) { 110 System.err.println(e); 111 } 112 } 113 114 118 private void buildURLWindow() { 119 builded = true; 120 jep = new JEditorPane(); 121 f = new JFrame("Simple Web Browser"); 122 contentPane = (JPanel) f.getContentPane(); 123 scrollPane = new JScrollPane(jep); 124 } 125 126 130 public void run() { 131 while (true) { 132 if (showURL == true) { 133 this.setPage(); 134 showURL = false; 135 } 136 try { 137 Thread.sleep(100); 138 } catch (InterruptedException e) { 139 e.printStackTrace(); 140 } 141 } 142 } 143 } 144 145 | Popular Tags |