I'm trying to create a TextArea
,
@FXML
private TextArea ta;
and what I'm trying to get :
for (int i=1; i<=5; i++) {
ta.setText(" Field " + i + "\n");
}
but it only show the last line : Field 5
.
Can anyone help. Thanks in advance.
To append text to JTextArea, use the component’s append () method. Let’sa say the following is our text in the JTextArea - JTextArea textArea = new JTextArea ("The text added here is just for demo. "+ " This demonstrates the usage of JTextArea in Java. ");
To add text to a textarea, access the value property on the element and set it to its current value plus the text to be appended, e.g. textarea.value += 'Appended text'. The value property can be used to get and set the content of a textarea element.
We can use jQuery to append text to a textarea field simply by getting the text from the textareausing the jQuery val()method and then adding our new text to this value. var newText = "This is our text we want to append"; var userInput = $("textarea").val(); userInput = userInput + " " + newText; $("textarea").val(userInput);
If you don't need to ask the user whether to replace or append the text, use the following simplified function: This example appends the new text to the existing text. To replace the existing text instead, remove the "+" sign.
The method .setText()
puts only one value into the field. If a value exists, the old one will be replaced. Try:
private StringBuilder fieldContent = new StringBuilder("");
for (int i=1;i<=5;i++)
{
//Concatinate each loop
fieldContent.append(" Field "+i+"\n");
}
ta.setText(fieldContent.toString());
That is one way to achive it.
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