1 16 17 package sample.google.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 34 37 protected static boolean showURL = false; 38 39 42 private static JEditorPane jep; 43 44 47 private static JFrame f; 48 private static JPanel contentPane; 49 private static JScrollPane scrollPane; 50 51 54 private static boolean builded = false; 55 56 59 private static String currentURL; 60 61 64 public LinkFollower() { 65 66 } 67 68 74 public void hyperlinkUpdate(HyperlinkEvent evt) { 75 if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 76 77 78 79 if (builded == false) { 80 buildURLWindaw(); 81 } 82 try { 83 currentURL = evt.getURL().toString(); 84 System.out.println(currentURL); 85 showURL = true; 87 88 } catch (Exception e) { 89 System.out.println("ERROR: Trouble fetching url"); 90 } 91 } 92 } 93 94 98 protected void setPage() { 99 jep.setEditable(false); 100 jep.addHyperlinkListener(new LinkFollower()); 101 f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 102 103 contentPane.setLayout(new BorderLayout()); 104 contentPane.setPreferredSize(new Dimension(400, 100)); 105 contentPane.add(scrollPane, BorderLayout.CENTER); 106 107 f.pack(); 108 f.setSize(640, 360); 109 f.setVisible(true); 110 try { 111 jep.setPage(currentURL); 112 } catch (IOException e) { 114 System.err.println(e); 115 } 116 } 117 118 122 private void buildURLWindaw() { 123 builded = true; 124 jep = new JEditorPane(); 125 f = new JFrame("Simple Web Browser"); 126 contentPane = (JPanel) f.getContentPane(); 127 scrollPane = new JScrollPane(jep); 128 } 129 130 134 public void run() { 135 while (true) { 136 if (showURL == true) { 137 this.setPage(); 138 showURL = false; 139 } 140 try { 141 Thread.sleep(100); 142 } catch (InterruptedException e) { 143 e.printStackTrace(); 144 } 145 } 146 } 147 } 148 149 | Popular Tags |