This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Reading JTextField Using AWT Event Queue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Reading JTextField Using AWT Event Queue" Watch "Reading JTextField Using AWT Event Queue" New topic
Author

Reading JTextField Using AWT Event Queue

Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

If I am making a request to read a value in a JTextField using tthe AWT Event Queue and storing it in a local variable, the value is not immediately available. I have to wait somehow to know it has been written from the Even Dispatch Thread to the local variable before using it in another thread. I am not sure how to synchronize this. Does anyone have a tip to point me in the right direction?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
It all depends on what the purpose of this is.
A couple of possible solutions are:
1. You could have your other thread use EventQueue.invokeAndWait to get the value from the JTextField.
2. You could write the value into a BlockingQueue on the event thread and have the other thread call the queue's take() method to get/wait for the value.

Note these are just 2 possible solutions I picked at random, if you can give full details of what you are trying to achieve we may be able to suggest a better solution for your scenario.
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

Yeah, invokeAndWait will work! I have not used that before, only invokeLater. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading JTextField Using AWT Event Queue
 
Similar Threads
If I don't need simultanius threads of the same object?
TextListener in JTextField ???
applet CAB file
Why does this throw a NullPointerException?
JTextField validation / Document Listener