Demonstrating the HyperlinkListener
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class HyperlinkTest {
public static void main(String args[]) {
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
final JEditorPane ep = new JEditorPane();
try {
ep.setPage("http://www.java2s.com");
} catch (IOException e) {
System.err.println("Bad URL: " + e);
System.exit(-1);
}
HyperlinkListener listener = new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
ep.setPage(e.getURL());
} catch (IOException ioe) {
System.err.println("Error loading: " + ioe);
}
}
}
};
ep.addHyperlinkListener(listener);
ep.setEditable(false);
JScrollPane pane = new JScrollPane(ep);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(640, 480);
frame.show();
}
}
Related examples in the same category