galica
galica

Reputation: 147

Refreshing GUI by another thread in java (swing)

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.

Upvotes: 10

Views: 37330

Answers (2)

unholysampler
unholysampler

Reputation: 17321

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
   ...
}

Upvotes: 26

mdma
mdma

Reputation: 57707

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.

Upvotes: 11

Related Questions