This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Runnable Thread to update GUI components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Runnable Thread to update GUI components" Watch "Runnable Thread to update GUI components" New topic

Runnable Thread to update GUI components

Pablo Mino

Joined: Aug 11, 2009
Posts: 28
Hi, I have a GUI with some fields and they get updated when I start a class that implements Runnable. I'm being forced to send the components in the class constructor in order for them to be used on the Runnable Thread side. For instance lets say I have a Jlabel that is a counter and that in my Runnable class I got the integer value ++, so I pass the JLabel to the Runnable class because otherwise the value won't change with time.

Is this the right way? because having swing components in the logic layer looks odd...
Pablo Mino

Joined: Aug 11, 2009
Posts: 28
Ok I found a way to pass things between threads by using volatile.

I got 5 different messages that need to be passed to the GUI and not mentioning the volatile boolean that stops the thread. Is this the way?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43905
Pablo Mino wrote:. . . Is this the way?

You must not access Swing from any thread but the Event Despatch Thread, because Swing components are not thread-safe. See this.

I am moving this thread to the GUIs forum.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2372
You can also use a Swing Worker. Read the section from the Swing tutorial on Concurrency for a complete discussion of this issue.
Have you checked out Aspose?
subject: Runnable Thread to update GUI components
It's not a secret anymore!