import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import javax.swing.text.html.HTMLEditorKit.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class HtmlDemo extends JPanel {
JEditorPane thePane;
JTextArea htmlTextArea;
StringReader aReader;
MyCallback cb;
ParserDelegator aParser;
public HtmlDemo() {
super();
String initialText = "\n" +
"
\n" +
"Color and font test:\n" +
"\n" +
"- red\n" +
"
- blue\n" +
"
- green\n" +
"
- small\n" +
"
- large\n" +
"
- italic\n" +
"
- bold\n" +
"
\n" +
"\n" +
"\n";
htmlTextArea = new JTextArea(10, 20);
htmlTextArea.setText(initialText);
JScrollPane scrollPane = new JScrollPane(htmlTextArea);
JButton changeTheLabel = new JButton("Change the content");
changeTheLabel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thePane.setText(htmlTextArea.getText());
try {
aParser.parse(aReader, cb, false);
} catch (Exception ep) {
System.err.println("Could not parse again");
}
}
});
changeTheLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
aReader = new StringReader(initialText);
cb = new MyCallback();
aParser = new ParserDelegator();
try {
aParser.parse(aReader, cb, false);
} catch (Exception e) {
System.err.println("Couldn't parse");
}
try {
thePane = new JEditorPane("text/html", initialText);
} catch (Exception e) {
System.err.println("Couldn't create editor pane");
}
thePane.setEditable(false);
JScrollPane theScrollPane = new JScrollPane(thePane);
theScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
theScrollPane.setPreferredSize(new Dimension(200, 200));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(
"Edit the HTML, then click the button"),
BorderFactory.createEmptyBorder(10,10,10,10)));
leftPanel.add(scrollPane);
leftPanel.add(Box.createVerticalStrut(10));
leftPanel.add(changeTheLabel);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("A label with HTML"),
BorderFactory.createEmptyBorder(10,10,10,10)));
rightPanel.add(theScrollPane);
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(leftPanel);
add(Box.createHorizontalStrut(10));
add(rightPanel);
}
public static void main(String args[]) {
JFrame f = new JFrame("HtmlDemo");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.getContentPane().add(new HtmlDemo());
f.pack();
f.setVisible(true);
}
}
class MyCallback extends HTMLEditorKit.ParserCallback {
public void handleStartTag(HTML.Tag tag, MutableAttributeSet attribs,
int pos) {
String fontcolor = "";
if (tag.equals(HTML.Tag.FONT)) {
fontcolor=(String)attribs.getAttribute(HTML.Attribute.COLOR);
if (fontcolor == null) {
return;
}
System.out.println("A font color is " + fontcolor
+"(" + pos + ")");
}
}
}