Listening for Hyperlink Events from a JEditorPane Component : Web Browser « Swing « Java Tutorial






import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class Main {
  public static void main(String[] argv) throws Exception{

    String url = "http://java.sun.com";
    JEditorPane editorPane = new JEditorPane(url);
    editorPane.setEditable(false);
    editorPane.addHyperlinkListener(new MyHyperlinkListener());
  }
}

class MyHyperlinkListener implements HyperlinkListener {
  public void hyperlinkUpdate(HyperlinkEvent evt) {
    if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      JEditorPane pane = (JEditorPane) evt.getSource();
      try {
        // Show the new page in the editor pane.
        pane.setPage(evt.getURL());
      } catch (IOException e) {
      }
    }
  }
}








14.36.Web Browser
14.36.1.Browser by JEditorPaneBrowser by JEditorPane
14.36.2.Listening for Hyperlink Events from a JEditorPane Component