2010-10-20 20 views
6

Así que he creado mi propia clase de panel de texto (extendiendo JTextPane) y estoy usando el método a continuación para agregarle texto. Sin embargo, el panel debe ser editable para que agregue el texto, pero esto permite que el usuario edite lo que está en el panel también.¿Agregar texto a un JTextPane sin que el usuario pueda editarlo?

¿Alguien me puede decir cómo agregar texto al panel sin permitir que el usuario manipule lo que hay allí?

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

Respuesta

6

actualizar el documento directamente:

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

entiendo eso, pero ¿cómo iba a añadir texto al final del documento? –

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); 
Cuestiones relacionadas