2010-10-20 17 views
6

Stworzyłem więc własną klasę okienka tekstowego (rozszerzając JTextPane) i używam poniższej metody, aby dodać do niej tekst. Panel musi jednak być edytowalny, aby można było dodać tekst, ale to pozwala użytkownikowi edytować także to, co jest w okienku.Dodawanie tekstu do JTextPane bez konieczności jego edycji przez użytkownika?

Czy ktoś może mi powiedzieć, jak dodać tekst do panelu, nie pozwalając użytkownikowi manipulować tym, co jest?

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()); 
} 

Odpowiedz

6

Aktualizacja dokumentu bezpośrednio:

StyledDocument doc = textPane.getStyledDocument(); 
doc.insertString("text", doc.getLength(), attributes); 
3
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!"); 
+0

Rozumiem to, ale jak bym dołączyć tekst na końcu dokumentu? –

0

Ok Take 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); 
Powiązane problemy