vamp658
vamp658

Reputation: 235

How to add text to a textArea instead of replacing it

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.

Upvotes: 15

Views: 83055

Answers (3)

DadViegas
DadViegas

Reputation: 2291

You can use the append method like this:

textArea.append(additionalText);

Upvotes: 27

Pawel Solarski
Pawel Solarski

Reputation: 1048

void append(JTextArea area, String newText){
        area.setText(area.getText() + newText)
}

Upvotes: -3

Kalecser
Kalecser

Reputation: 1143

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

Upvotes: 4

Related Questions