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.
Use textArea. append("text") , but I recomend JTextPane for more control like color, selection, etc.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With