6

そこで、独自のテキストペインクラス(JTextPaneを拡張)を作成し、以下のメソッドを使用してテキストを追加しています。ただし、テキストを追加するには、ペインを編集可能にする必要がありますが、これにより、ユーザーはペインの内容を編集することもできます。

誰かが、ユーザーにそこにあるものを操作させずに、ペインにテキストを追加する方法を教えてもらえますか?

public void appendColor(Color c, String s) { 
    StyleContext sc = StyleContext.getDefaultStyleContext(); 
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

    int len = getDocument().getLength(); 

    setCaretPosition(len); 

    setCharacterAttributes(aset, false);

    replaceSelection(s); 

    setCaretPosition(getDocument().getLength());
} 
4

4 に答える 4

9

ドキュメントを直接更新します。

StyledDocument doc = textPane.getStyledDocument();
doc.insertString("text", doc.getLength(), attributes);
于 2010-10-20T02:41:05.780 に答える
4
JTextPane pane = new JTextPane();
pane.setEditable(false);  // prevents the user from editting it.
// programmatically put this text in the TextPane
pane.setText("Hello you can't edit this!");
于 2010-10-20T02:24:37.863 に答える
1

OKテイク2:

JTextPane pane = new JTextPane();
pane.setEditable(true);
DefaultStyledDocument document = (DefaultStyledDocument)pane.getDocument();
document.insertString( "Hello you can't edit this!", document.getEndPosition().getOffset(), null );
于 2010-10-20T02:41:39.257 に答える
1
JTextPane myTextArea = new JTextPane();
myTextArea.setEditable(false);  
于 2018-05-04T15:39:37.730 に答える