问题
How can I add text to a JTextArea
instead of replacing all of it?
I know about setText(String)
but other than that I'm a bit lost.
回答1:
You can use the append method like this:
textArea.append(additionalText);
回答2:
To insert string at any position you can use the component's Document.
public static void main(String[] args) throws BadLocationException {
JTextField f = new JTextField("foo bar");
int offset = 7;
String str = " baz";
f.getDocument().insertString(offset, str, SimpleAttributeSet.EMPTY);
System.out.println(f.getText());
}
回答3:
void append(JTextArea area, String newText){
area.setText(area.getText() + newText)
}
来源:https://stackoverflow.com/questions/12216494/how-to-add-text-to-a-textarea-instead-of-replacing-it