4

常に追加される (ログ ファイルに似た) オブジェクト (モデル) のリストがあり、JEditorPane (ビュー) にリッチ テキストとして表示したいと考えています。どうすればそれらを接着できますか?

http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#documentは、使用するのに十分な情報を提供していないようです。

4

4 に答える 4

2

簡単な解決策の 1 つは、モデル内の各オブジェクトを HTML に変換し、文字列を追加して、JEditorPane で設定できる HTML ドキュメントを作成することです。

于 2009-03-13T09:39:18.307 に答える
2

DefaultStyledDocument次のものと一緒に使用できますAttributeSet

SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBold(attr , true);
StyleConstants.setForeground(attr, Color.RED); 
document.insertString(document.getLenght(),"yourstring", attr))
于 2009-03-13T14:59:17.930 に答える
0

わかりました。最も簡単な方法は、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));

テキスト ペインが最後までスクロールしない場合があるため、一貫性のない結果が得られます。

于 2009-03-19T14:01:47.367 に答える