package browser; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; public class Browser extends JFrame { private static final long serialVersionUID = 1L; public static final String LINK_OPEN_STRING = "Open "; public static final String LINK_DEFAULT_STRING = " "; public static final String FAILURE_URL = "file:Failure.html"; private JEditorPane pane; /** * The constructor runs the browser. It displays the main frame with the * fetched initialPage * * @param initialPage * the first page to show */ public Browser(String initialPage) { // set up the editor pane pane = new JEditorPane(); pane.setEditable(false); JScrollPane scrollPane = new JScrollPane(pane); // label to show links final JLabel label = new JLabel(LINK_DEFAULT_STRING); pane.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent evt) { // Mouse enters on the link if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) label.setText(LINK_OPEN_STRING + evt.getURL().toString()); // Link activate if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { setPage(evt.getURL().toString()); } // Mouse exits the link if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) label.setText(LINK_DEFAULT_STRING); } }); Container content = getContentPane(); content.add(scrollPane, BorderLayout.CENTER); content.add(label, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setPage(initialPage); setSize(960, 720); pack(); setVisible(true); } protected void setPage(String url) { try { pane.setPage(url); } catch (Exception e) { try { pane.setPage(FAILURE_URL); } catch (Exception e2) { System.err.println(e2.getMessage()); } } } /** Create a Browser object. Use the command-line url if given */ public static void main(String[] args) { String initialPage = "http://www.google.fr"; if (args.length > 0) initialPage = args[0]; new Browser(initialPage); } }