This section provides a tutorial example on how to use the setContentType() method of the javax.swing.JEditorPane class to set force the editor pane to display content in HTML format.
Now I want to try the "text/html" content type with the javax.swing.JEditorPane class.
In this tutorial example, I want to add a new menu, "View", with two functions, "HTML" and "Plain".
If you click HTML, the text in the editor pane will be displayed in HTML format.
If you click Plain, the text in the editor pane will be displayed in plain text format.
Of course, this is done by using the setContentType() of the JEditorPane class.
/**
* JEditorPaneHtml.java
* Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
*/
import java.io.*;
import java.nio.*;
import java.awt.event.*;
import javax.swing.*;
public class JEditorPaneHtml implements ActionListener {
JFrame myFrame = null;
JEditorPane myPane = null;
public static void main(String[] a) {
(new JEditorPaneHtml()).test();
}
private void test() {
myFrame = new JFrame("JEditorPane HTML Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(300,300);
myPane = new JEditorPane();
myPane.setContentType("text/html");
myPane.setText(
"<p><b>JEditorPane</b> is a text component to edit various kinds of"
+" content.\n\nThis component uses implementations of the"
+" EditorKit to accomplish its behavior.</p>");
myFrame.setContentPane(myPane);
JMenuBar myBar = new JMenuBar();
JMenu myMenu = getFileMenu();
myBar.add(myMenu);
myMenu = getViewMenu();
myBar.add(myMenu);
myFrame.setJMenuBar(myBar);
myFrame.setVisible(true);
}
private JMenu getFileMenu() {
JMenu myMenu = new JMenu("File");
JMenuItem myItem = new JMenuItem("Open");
myItem.addActionListener(this);
myMenu.add(myItem);
myItem = new JMenuItem("Save");
myItem.addActionListener(this);
myMenu.add(myItem);
return myMenu;
}
private JMenu getViewMenu() {
JMenu myMenu = new JMenu("View");
ButtonGroup myGroup = new ButtonGroup();
JMenuItem myItem = new JRadioButtonMenuItem("HTML");
myItem.setSelected(true);
myItem.addActionListener(this);
myMenu.add(myItem);
myGroup.add(myItem);
myItem = new JRadioButtonMenuItem("Plain");
myItem.addActionListener(this);
myMenu.add(myItem);
myGroup.add(myItem);
return myMenu;
}
public void actionPerformed(ActionEvent e) {
String cmd = ((AbstractButton) e.getSource()).getText();
String text = null;
try {
if (cmd.equals("Open")) {
FileReader in = new FileReader("JEditorPane.txt");
char[] buffer = new char[1024];
int n = in.read(buffer);
text = new String(buffer, 0, n);
myPane.setText(text);
in.close();
} else if (cmd.equals("Save")) {
FileWriter out = new FileWriter("JEditorPane.txt");
out.write(myPane.getText());
out.close();
} else if (cmd.equals("HTML")) {
text = myPane.getText();
myPane.setContentType("text/html");
myPane.setText(text);
} else if (cmd.equals("Plain")) {
text = myPane.getText();
myPane.setContentType("text/plain");
myPane.setText(text);
}
} catch (Exception f) {
f.printStackTrace();
}
}
}
If you run this example, you will see a text editor pane displayed
with the initial text content and a menu bar with the "File" menu and the "View" menu.
Initially, the content is displayed in HTML format. the <b> tag is properly processed.
You can <b>The End</b> at the end.
If you click the "Plain" command in the "View" menu, the content will be displayed in plain text,
the original HTML source code as shown in the picture below:
Interesting notes about this tutorial example:
setContentType() method will actually remove the current text content in the editor pane.
This is why I have save the content into a variable first.
Sample programs listed in this section have been tested with JDK 1.6.0.