Listening for Hyperlink Events from a JEditorPane Component

Hyperlink events are fired by a JEditorPane when the user clicks on a hyperlink.
try { String url = "http://java.sun.com"; JEditorPane editorPane = new JEditorPane(url); editorPane.setEditable(false); editorPane.addHyperlinkListener(new MyHyperlinkListener()); } catch (IOException e) { } 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) { } } } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.