• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting data from a Thread

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Barry,
You could do something like this:


Note: The call to your other class is executed by the thread itself.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic