• 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

Notify swing gui of an event

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a thread that kicks off a search in one of my GUIs. When that thread is updating it needs to send objects to the gui. I would like to seperate this logic from the actual GUI class if possible. I.E. I can have this class as an inner class but again I don't really want it inside the GUI class. What is the easiest way to notify the gui from my thread that the thread has some information for the gui.

Thanks,
Billy
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read into SwingUtilities.invokeLater . That will help you update the GUI from the thread when there is something to update.
 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure that is what I am looking for.



Runnable code in another class i.e. not an inner class to MyFrame



So I could construct my runnable object with a MyFrame object, but I am not sure if that is a best practice or if there is some event I can fire off in the thread that MyFrame will listen to such that it will receive updates.

Thanks!
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's exactly what you need.

Take a look at this example:

http://www.java2s.com/Code/Java/Swing-JFC/Swinginvokelater.htm
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SwingUtilities.invokeLater is only used to put any Swing update code on the Swing thread (EDT).

your MyRunnable class needs a reference to what is to be updated,
or it can be an inner class that has access to the component/s needing updating

here's a simple demo of passing a reference
(it will probably run OK without the SwingUtilities.invokeLater, but you may get intermittent problems)

 
reply
    Bookmark Topic Watch Topic
  • New Topic