常に追加される (ログ ファイルに似た) オブジェクト (モデル) のリストがあり、JEditorPane (ビュー) にリッチ テキストとして表示したいと考えています。どうすればそれらを接着できますか?
http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#documentは、使用するのに十分な情報を提供していないようです。
常に追加される (ログ ファイルに似た) オブジェクト (モデル) のリストがあり、JEditorPane (ビュー) にリッチ テキストとして表示したいと考えています。どうすればそれらを接着できますか?
http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#documentは、使用するのに十分な情報を提供していないようです。
簡単な解決策の 1 つは、モデル内の各オブジェクトを HTML に変換し、文字列を追加して、JEditorPane で設定できる HTML ドキュメントを作成することです。
DefaultStyledDocument
次のものと一緒に使用できますAttributeSet
。
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBold(attr , true);
StyleConstants.setForeground(attr, Color.RED);
document.insertString(document.getLenght(),"yourstring", attr))
わかりました。最も簡単な方法は、JTextPane を拡張することでした。拡張クラスは、基礎となるリストを作成および管理しました。フォーマットの変更 (新しい色など) では、リストはデータを完全に再フォーマットします。唯一の問題は、自動スクロールが 100% 信頼できるものではないことです。
Container parent = getParent();
// get the parent until scroll pane is found
while (parent != null && !(parent instanceof JScrollPane)) {
parent = parent.getParent();
}
if (parent != null) {
JScrollPane scrollPane = (JScrollPane)parent;
scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
}
と
scrollRectToVisible(new Rectangle(0, getHeight() - 2, 1, 1));
テキスト ペインが最後までスクロールしない場合があるため、一貫性のない結果が得られます。