Java Tutorial/Swing Event/HyperlinkListener
HyperlinkListener Example
import java.io.IOException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
class ActivatedHyperlinkListener implements HyperlinkListener {
JEditorPane editorPane;
public ActivatedHyperlinkListener(JEditorPane editorPane) {
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
System.out.println("Error following link, Invalid link");
editorPane.setDocument(doc);
}
}
}
}
public class EditorPaneSample {
public static void main(String args[]) {
JFrame frame = new JFrame("EditorPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
JEditorPane editorPane = new JEditorPane("http://www.google.ru");
editorPane.setEditable(false);
HyperlinkListener hyperlinkListener = new ActivatedHyperlinkListener(editorPane);
editorPane.addHyperlinkListener(hyperlinkListener);
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane);
} catch (IOException e) {
System.err.println("Unable to load: " + e);
}
frame.setSize(640, 480);
frame.setVisible(true);
}
}
JEditorPane and HyperlinkEvent
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class MainClass extends JFrame {
protected JEditorPane mEditorPane;
protected JTextField mURLField;
public MainClass() {
super();
JToolBar urlToolBar = new JToolBar();
mURLField = new JTextField(40);
urlToolBar.add(new JLabel("Location:"));
urlToolBar.add(mURLField);
frame.add(urlToolBar, BorderLayout.NORTH);
mEditorPane = new JEditorPane();
mEditorPane.setEditable(false);
frame.add(new JScrollPane(mEditorPane), BorderLayout.CENTER);
openURL("http://www.jexp.ru");
mURLField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
openURL(ae.getActionCommand());
}
});
mEditorPane.addHyperlinkListener(new LinkActivator());
setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected void openURL(String urlString) {
try {
URL url = new URL(urlString);
mEditorPane.setPage(url);
mURLField.setText(url.toExternalForm());
} catch (Exception e) {
System.out.println("Couldn"t open " + urlString + ":" + e);
}
}
class LinkActivator implements HyperlinkListener {
public void hyperlinkUpdate(HyperlinkEvent he) {
HyperlinkEvent.EventType type = he.getEventType();
if (type == HyperlinkEvent.EventType.ACTIVATED)
openURL(he.getURL().toExternalForm());
}
}
public static void main(String[] args) {
new MainClass().setVisible(true);
}
}