Rendimiento inconsistente aplicando ForegroundActions en un JEditorPane al leer HTML
Estoy creando un editor HTML usandoJEditorPane
, pero tengo problemas de rendimiento inconsistentes con las acciones de primer plano. Tengo una versión simplificada de mi editor a continuación que tiene tres acciones: cambiar el color de la fuente a rojo o azul, o cambiar el tamaño de la fuente. Ahora usando el siguiente archivo 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>
a veces puedo resaltar algo de texto en el editor y presionar los botones de color rojo o azul, y funciona bien, es decir, cambia de color. En otras ocasiones (es decir, si cierro mi JVM y lo vuelvo a iniciar) el color no cambiará HASTA que aplique unaStyledEditorKit.FontSizeAction
en el mismo texto.
¿Hay algo que falta en cómo estoy aplicando elForegroundActions
? ¿O podría ser algún error de Java?
Código a continuación:
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){}
}
}