Forums Register Login

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
I've never won anything before. Not even a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1547 times.
Similar Threads
No output from JTextArea while using Robot with Jsch
Key listener using jsp and servlet rather than using JFrame(windows form)
Recognising when nothing entered in JTextField
Help with GlassPane intercepting Key events
Trouble implementing multiple event handlers
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:16:14.