Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a new line to the end of a JtextArea

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.

like image 734
Johanna Avatar asked Jan 18 '10 17:01

Johanna


People also ask

How do I add text to JTextArea?

Use textArea. append("text") , but I recomend JTextPane for more control like color, selection, etc.

What is line wrap in JTextArea?

To wrap the lines of JTextArea we need to call the setLineWrap(boolean wrap) method and pass a true boolean value as the parameter. The setWrapStyleWord(boolean word) method wrap the lines at word boundaries when we set it to true .

Is JTextArea editable?

The following example shows you how to set the property of the JTextArea so that it cannot be edited or modified. To make the JTextArea not editable call the setEditable() method and pass a false value as the parameter.

What are rows and columns in JTextArea?

JTextArea(int row, int column) : constructs a new text area with a given number of rows and columns. JTextArea(String s, int row, int column) : constructs a new text area with a given number of rows and columns and a given initial text.


1 Answers

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.

like image 113
jjnguy Avatar answered Oct 11 '22 11:10

jjnguy