9

I have a main program, in which GUI is based on swing and depending on one of four states the GUI elements have different parameters.

public class Frame extends JFrame implements Runnable {
Status status = 1;
...
@Override
public void run() {
    switch (status) {
        case 1:
        ...
        case 2:
        ...
}

public void updateGUI(Status status) {
   this.status = status;
   SwingUtilities.invokeLater(this);
}

And if I want to refresh the GUI invokes only updateGUI with appropriate parameter, and everything is fine. But the program also creates an additional thread, which after processing the relevant data should change the GUI main program. Unfortunately I can not in this thread call the method updateGUI (..).

I know that I can use invokeLater or SwingWorker to refresh but there are more than 10 elements so I would rather use the method udpateGUI ().

I will be grateful for any hint.

galica
  • 137
  • 1
  • 2
  • 10
  • Please take a look at the answers about events & event bus in this question: http://stackoverflow.com/questions/7131484/how-to-pass-the-message-from-working-thread-to-gui-in-java/7131574#7131574 – omnomnom Aug 29 '11 at 11:19
  • Why can't you call updateGUI from the other thread? Other than the non-synchronized access to the status variable, I don't see why you couldn't use this method from the other thread. – JB Nizet Aug 29 '11 at 11:19
  • if I invoke Frame f = new Frame(); f.udpateGUI(..); then of course nothing happend. How else I can invoke that method? – galica Aug 29 '11 at 11:22
  • 2
    Pass the frame to update to your background task (in its constructor, for example) – JB Nizet Aug 29 '11 at 11:28

2 Answers2

24

Here is a little snippet you can add to a method to ensure it executes in the the GUI thread. It relies on isEventDispatchThread().

public void updateGUI(final Status status) {
   if (!SwingUtilities.isEventDispatchThread()) {
     SwingUtilities.invokeLater(new Runnable() {
       @Override
       public void run() {
          updateGUI(status);
       }
     });
     return;
   }
   //Now edit your gui objects
   ...
}
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
unholysampler
  • 17,141
  • 7
  • 47
  • 64
11

If you make the status field thread safe, then you can call setStatus directly from your background thread. To make the status thread-safe, put changes in a synchronize block, and make the variable volatile so updates on other threads become visible.

E.g.

public class Frame extends JFrame implements Runnable {
private volatile Status status = 1;
...
@Override
public void run() {
    switch (status) {
        case 1:
        ...
        case 2:
        ...
}

public void updateGUI(Status status) {
   setStatus(status);
   SwingUtilities.invokeLater(this);
}

private synchronized void setStatus(Status status) {
   this.status = status;
}

With these changes in place, it's ok to call setStatus from any thread.

mdma
  • 56,943
  • 12
  • 94
  • 128