Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending text into TextArea

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.

like image 410
zoey3 Avatar asked Apr 16 '15 11:04

zoey3


People also ask

How to append text to jtextarea in Java?

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. ");

How do I add text to a textarea in HTML?

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.

How to append text to a textarea field using jQuery?

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);

How do I append or replace text in a text box?

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.


1 Answers

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.

like image 149
Reporter Avatar answered Oct 21 '22 17:10

Reporter