HTMLEditorKit によってサポートされる JEditorPane に<br>
タグとそれに続く空の行が含まれている場合、その行は正しくレンダリングされず、キャレットも正しく処理されません。次のサンプル コードを検討してください。
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);
}
}
<br>
タグの後の空行は表示されません。キャレットが「a」文字の左側に配置され、上矢印キーが押されると、キャレットが消えます。
「上」を押す前に:
「上」を押した後:
「test」と「a」の間の距離が小さすぎて、キャレットが消えていることに注意してください。
次にテキストを入力すると、欠落している空の行が表示されます。
問題は、空の行が高さ 0px でレンダリングされるため、キャレットがその行にある場合はキャレットを含めて表示されないことです。行にコンテンツが含まれると、そのコンテンツはゼロ以外の行の高さを強制します。
この問題の簡単な回避策/修正方法を知っていますか? 最悪の場合、独自のエディター キット( JEditorPane でのカスタム行ラッピングについてはこちらとこちらも参照) やカスタム タグ(こちらも参照) を作成する必要があると思います。