Search code examples
javaswingnetbeansjbuttonjtextarea

To append into jtextarea from another jtextarea using jbutton


I have developed a swing GUI in which I try to populate a text area with the text from another text area using button.

Code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {     

jTextArea2.setText(4.0+" "+jTextArea1.getText().trim());
jTextArea1.setText("")
}

It is working fine if I enter the text first time in TextArea1 and hit the button to populate the TextArea2. But if I enter a new text in TextArea1 and hit the button instead of appending it to the TextArea2 it replaces the text.

Please suggest me how to correct the same.


Solution

  • You should invoke append no setText

    jTextArea2.append(4.0+" "+jTextArea1.getText().trim());