Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextArea Input from Event Listener

Status
Not open for further replies.

Sweats

Programmer
Nov 15, 2006
6
US
After a button is clicked I would like text to be displayed in a text area that I have on the same GUI. I have accomplished this but after each new entry to the output text area, the line previously there gets erased. I understand that this is the normal functionality of setText. I was wondering if there was anyway that I can keep entering input but be able to keep the previous entries, in other words not have the previous input erased.

code inside event listener:
output_TextArea.setText("Make a left turn \n");

Any help would be much appreciated. Thanks.
 
Keep the original input in a StringBuffer and append the new input to it. Then setText on the JTextArea with the whole contents of the StringBuffer.

Or, do a getText from the JTextArea into a StringBuffer, append the new input, the write whole contents of StringBuffer back to the JTextArea.

Code:
StringBuffer sb = new StringBuffer(myTextArea.getText());
sb.append("some new input");//substitute your new input here
myTextArea.setText( sb.toString() );

... or extend the JTextArea and add a method like appendText(String text) which internally does the above.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top