When a JEditorPane backed by an HTMLEditorKit contains a <br>
tag followed by an empty line, that line is not rendered correctly and the caret is not handled correctly. Consider this sample code:
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class HTMLEditorTest {
public static void main(String[] args) throws IOException, BadLocationException {
JFrame frame = new JFrame();
Reader stringReader = new StringReader("test<br><p>a");
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlKit.read(stringReader, htmlDoc, 0);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(htmlKit);
editorPane.setDocument(htmlDoc);
frame.getContentPane().add(BorderLayout.CENTER, new JScrollPane(editorPane));
frame.setBounds(100, 100, 500, 400);
frame.setVisible(true);
}
}
The empty line after the <br>
tag is not rendered. When the caret is positioned left of the 'a' char and the arrow up key is pressed, the caret disappears:
Before pressing 'up':
After pressing 'up':
Note that the distance between 'test' and 'a' is too small, and the caret has disappeared.
When you then enter text, the missing empty line becomes visible:
The problem seems to be that the empty line is rendered with a height of 0px, and thus is not visible, including the caret if it is on that line. Once the line has content, that content forces a non-zero line height.
Do you know a simple workaround / fix for this problem? I reckon in the worst case, I have to write my own editor kit (see also here and here for custom line wrapping in JEditorPane) and/or custom tag (also here).