简体   繁体   中英

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.

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.

Are you using JTextArea 's append(String) method to add additional text?

JTextArea txtArea = new JTextArea("Hello, World\n", 20, 20);
txtArea.append("Goodbye Cruel World\n");

When you want to create a new line or wrap in your TextArea you have to add \\n (newline) after the text.

TextArea t = new TextArea();
t.setText("insert text when you want a new line add \nThen more text....);
setBounds();
setFont();
add(t);

This is the only way I was able to do it, maybe there is a simpler way but I havent discovered that yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM