• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runnable Thread to update GUI components

 
Pablo Mino
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo Mino wrote:. . . Is this the way?
No.

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
Posts: 2600
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use a Swing Worker. Read the section from the Swing tutorial on Concurrency for a complete discussion of this issue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic