This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Notify swing gui of an event  RSS feed

 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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)

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!