Listening for Hyperlink Events from a JEditorPane Component : Hyperlink « Network Protocol « Java






Listening for Hyperlink Events from a JEditorPane Component

 


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) {
      }
    }
  }
}

   
  








Related examples in the same category

1.How to change mouse cursor during mouse-over action on hyperlinks