I'm building an HTML editor using JEditorPane
, but I'm getting some inconsistent performance issues with Foreground Actions. I have a simplified version of my editor below that has three actions: change the font color to red or blue, or change the font-size. Now using the following testFile.html file:
<html>
<head><title>Title</title></head>
<body link="#0000FF" bgcolor="white">
<font size="4" face="arial" color="black">Some test text</font>
<font size="3" face="arial" color="black">Some new test text </font>
</body>
</html>
sometimes I can highlight some text in the editor, and press the red or blue color buttons, and it works fine i.e. it changes color. On other occasions (i.e. if I close my JVM and start it up again) the color will not change UNTIL I apply a StyledEditorKit.FontSizeAction
on the same text.
Is there something missing in how I'm applying the ForegroundActions
? Or might this be some Java bug?
Code below:
public class EditorTest extends JFrame{
private JEditorPane editorPane;
public EditorTest()
{
editorPane = new JEditorPane();
editorPane.setContentType("text/HTML");
getContentPane().add(editorPane, BorderLayout.CENTER);
editorPane.setEditorKit(new HTMLEditorKit());
Action a = new StyledEditorKit.ForegroundAction("RedColor", Color.RED);
editorPane.getActionMap().put("RedColor", a);
JToolBar bar = new JToolBar();
JButton button = new JButton("blue");
button.addActionListener(new StyledEditorKit.ForegroundAction (
"set-foreground-red", Color.blue));
bar.add(editorPane.getActionMap().get("font-size-12")).setText("12");
bar.add(button);
bar.add(editorPane.getActionMap().get("RedColor")).setText("Red");
getContentPane().add(bar, BorderLayout.NORTH);
setSize(650,600);
setVisible(true);
File file = new File("testFile.html");
FileReader reader = null;
try
{
reader = new FileReader(file);
editorPane.read(reader, null);
}
catch (IOException ex){}
}
}
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…