Add a new line to the end of a JtextArea

Johanna picture Johanna · Jan 18, 2010 · Viewed 144.2k times · Source

I have a text area with some text in it and I want to add some lines to it again, (the first lines + the other lines that I want to add) but it doesn't work.

The way I'm doing it right now erases the old text and shows just the new lines.

Answer

jjnguy picture jjnguy · Jan 18, 2010

Instead of using JTextArea.setText(String text), use JTextArea.append(String text).

Appends the given text to the end of the document. Does nothing if the model is null or the string is null or empty.

This will add text on to the end of your JTextArea.

Another option would be to use getText() to get the text from the JTextArea, then manipulate the String (add or remove or change the String), then use setText(String text) to set the text of the JTextArea to be the new String.