File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Getting data from a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Getting data from a Thread" Watch "Getting data from a Thread" New topic
Author

Getting data from a Thread

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
If a swing application is starting a Thread from which it will get data and populate some Swing control after the Thread finishes, how is this accomplished.

Example: The Swing application kicks off a thread with calls a database. After the database call is complete I want to fill a JTable. Note: the database call is in a different class.

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Originally posted by Barry Brashear:
If a swing application is starting a Thread from which it will get data and populate some Swing control after the Thread finishes, how is this accomplished.


It is accomplished the same way any two classes which need to communicate with each other does it. You need a reference from the instance to another -- meaning a way to get from the database object to the swing component.

There is another issue though. The swing utilites are not threads safe. Most operations need to be done from the event dispatching thread. To run something on the event thread, take a peek at the SwingUtilities class -- the invokeLater() and invokeAndWait() methods to be exact.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
Could you have the database object post an event that the swing object is
listening for? Then call a method containing the invokeLater to update the
swing components?

Thanks.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Barry,
You could do something like this:


Note: The call to your other class is executed by the thread itself.


With Best Regards,
Shyam Prasad Murarka
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
//Update your swing components here


There is exactly where you do NOT want to update your swing components. As Henry said above, the swing components are not thread safe. This is what he means by that.

However, you could fire an event that is noticed by your gui which will update the data in the event dispatch thread. You can look into the Observer/Observable pattern as well as the invokeLater() method.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The key is the invokeLater() or invokeAndWait() methods on SwingUtilities. That will put some command in a queue and invoke it on the GUI thread.

An event-driven observer-observable structure is a good thing for separation of concerns, but usually the event is generated by the worker thread and all code runs on the worker thread, so still need one of the invoke methods in the observer.

That's how I made my stuff run anyhow. Before I learned about SwingUtilities my updates from worker threads were unpredictable even with a nice pub-sub thing going on between the model & view. I changed to use the invokeLater method and all is well.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Pieter Schmidt
Greenhorn

Joined: May 01, 2005
Posts: 5
Originally posted by Barry Brashear:
Could you have the database object post an event that the swing object is
listening for? Then call a method containing the invokeLater to update the
swing components?

Thanks.


Have a look at this article.

http://liemur.co.uk/Articles/ThreadCommunication.html

It explains how to communicate between 2 threads using a mailbox pattern. There is also a download of sample code that shows how this is accomplished.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting data from a Thread
 
Similar Threads
Accessing datasource through a swing app
how to implement Ajax with swing
Oracle data fetch delay
using threads in swing
SWT question