Java Reference
In-Depth Information
// Retain reference to original
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
JOptionPane.showMessageDialog(frame, "Error following link",
"Invalid link", JOptionPane.ERROR_MESSAGE);
editorPane.setDocument(doc);
}
}
};
EventQueue.invokeLater(runner);
}
}
}
Tip Don't forget to make the JEditorPane read-only with a call to setEditable(false) . Otherwise,
the viewer acts as an editor.
Listing 15-16 is a complete example using our new ActivatedHyperlinkListener class. The
frame it creates looks like the page shown earlier in Figure 15-19, although in the figure, the
About link has been followed.
Listing 15-16. JEditorPane Example
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
public class EditorPaneSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("EditorPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
JEditorPane editorPane = new JEditorPane("http://www.google.com");
editorPane.setEditable(false);
HyperlinkListener hyperlinkListener =
new ActivatedHyperlinkListener(frame, editorPane);
editorPane.addHyperlinkListener(hyperlinkListener);
 
Search WWH ::




Custom Search