Добавление текста в JTextPane без возможности его редактирования пользователем? - PullRequest
5 голосов
/ 20 октября 2010

Итак, я создал свой собственный класс текстовых панелей (расширяющий 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 ]

7 голосов
/ 20 октября 2010

Обновите документ напрямую:

StyledDocument doc = textPane.getStyledDocument();
doc.insertString("text", doc.getLength(), attributes);
3 голосов
/ 20 октября 2010
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 голосов
/ 04 мая 2018
JTextPane myTextArea = new JTextPane();
myTextArea.setEditable(false);  
0 голосов
/ 20 октября 2010

Хорошо, возьми 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 );
...