In order to use editor panes and text panes, you need to understand the text system, which is described in Text Component Features. Several facts about editor panes and text panes are scattered throughout that section. Here we list the facts again and provide a bit more detail. The information here should help you understand the differences between editor panes and text panes, and when to use which.
- An editor pane or a text pane can easily be loaded with text from a URL using the
setPage
method. TheJEditorPane
class also provides constructors that let you initialize an editor pane from a URL. TheJTextPane
class has no such constructors. See Using an Editor Pane to Display Text From a URL for an example that uses this feature to load an uneditable editor pane with HTML-formatted text.
package textinout;
import javax.swing.*;
public class TextInOut extends JPanel{
private JEditorPane editorpane;
private String path = "files/html.htm";
private URL resURL;
public TextInOut() {
editorpane = new JEditorPane();
JScrollPane scroller = new JScrollPane(editorpane);
scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorpane.setEditable(false);
editorpane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
editorpane.setPage(e.getURL());
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "can not follow the link: " + e.getURL().toExternalForm(),
"", JOptionPane.ERROR_MESSAGE);
}
}
}
});
resURL = TextInOut.class.getResource(path);
try {
editorpane.setPage(resURL);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "resource can not be found: " + path);
}
Component add = this.add(scroller);
}
/**the main method is an option to launch this 'amphibien' application in JFrame*/
public static void main(String... arg) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame jf = new JFrame("Text in and out demo");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setPreferredSize(new Dimension(600, 600));
TextInOut tio = new TextInOut();
jf.add(tio);
jf.setVisible(true);
jf.pack();
}
}