• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing: JTextArea.append(), Alternative/Solutions?  RSS feed

 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is the set up to my GUI...



Simply, a JTextArea inside a JScrollPanel, The JScrollPanel @ BorderLayout.CENTER
with a JTextField @ BorderLayout.SOUTH

I'm making a text engine so...

1. Collect input from textField object.
2. Process input, build output
3. Print output to textArea object
4. Print input to textArea object
5. ???
6. Profit!

Anyways.

Here is the current problem I am tasked with.



From the listener upon enter press in the textField, the input if !empty is sent (in the future to processing beforehand) to the enlister which from there is sent to be printed in this method.



Now I know this won't work because the thread will get held up and I won't have the text printed until other tasks are finished.

I need to be able to do other things while text should be printed to the textArea, how do I go about this?

the .append() method is fantastic, but unfortunately I do not know how to make it so I can split off with another thread so it can finish itself up on its own
without having to wait on the thread to come back around to it because it results in my GUI locking up.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus Hirschbine wrote:...but unfortunately I do not know how to make it so I can split off with another thread so it can finish itself up on its own
without having to wait on the thread to come back around to it because it results in my GUI locking up.


Then you need to go through the Swing threading tutorial. My idea of what you might do: set the JTextArea to not-enabled (so the user can't interact with it any more), then start up a thread (following the Swing tutorial which I linked to) which does its business in the background and then updates the other component.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We usually discuss GUIs on a different forum so I shall move this discussion.

Have you tried using an action listener instead of a key listener for the enter key? Have a look through the Java® Tutorials and see whether you can sue an action listener on a text area.
 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I've got now:



From my tests it works, but perhaps there is a better method/optomizing? Thank you Clapman for those citations.

Ritchie, as for the action listener, what would be the advantages? I haven't had any problems with the key listener thus far to say the least.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better method? It would help to know what the "delay" parameter is for and why it can't always be zero. Perhaps it's a stub, to be replaced by some future heavy-duty processing?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!