Desempenho inconsistente aplicando ForegroundActions em um JEditorPane ao ler HTML
Estou construindo um editor de HTML usandoJEditorPane
, mas estou tendo alguns problemas de desempenho inconsistentes com as ações em primeiro plano. Eu tenho uma versão simplificada do meu editor abaixo que possui três ações: alterar a cor da fonte para vermelho ou azul ou alterar o tamanho da fonte. Agora, usando o seguinte arquivo testFile.html:
<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>
s vezes, posso destacar algum texto no editor e pressionar os botões vermelho ou azul, e funciona bem, ou seja, muda de cor. Em outras ocasiões (por exemplo, se eu fechar minha JVM e iniciá-la novamente), a cor não mudará ATÉ que eu aplique umStyledEditorKit.FontSizeAction
no mesmo texto.
Há algo faltando em como estou aplicando oForegroundActions
? Ou isso pode ser algum bug do Java?
Código abaixo:
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){}
}
}