The GUI does not update after calling the GUI method in another thread

advertisements

EDIT: Simplified my code.

This is the main class that initialises the GUI and Server threads.

public class Main {
    public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException, UnknownHostException {
    Runnable tGUI = new TransceiverGUI();
    Runnable server = new Server(tGUI);
    new Thread(tGUI).start();
    new Thread(server).start();
    }
}

Now, in my public class TransceiverGUI extends javax.swing.JFrame implements Runnable class, I have a method with the following:

protected boolean incomingFileRequest(String filename, long filesize, String user) throws InterruptedException, InvocationTargetException {
        /* .... code logic which executes .... */
        /* UPDATE GUI HERE DOES NOT WORK */
        LabelProgress.setForeground(Color.red);
        LabelProgress.setText("Receive Progress");
    }

Whenever incomingFileRequest is called from within the GUI class (from any EventListener) it works perfectly and the GUI is updated.

However, when I call incomingFileRequest from the Server class/thread, the code runs and returns the correct value, but the GUI does not update.

The server thread calls it like this: // POPUP with request boolean answer = gui.incomingFileRequest(message.getMessage(), message.getFileSize(), message.senderIPAddress);

I have placed the code that updates the GUI in a block that creates a new thread like this:

Thread t = new Thread() {
public void run() {
   LabelProgress.setForeground(Color.red);
   LabelProgress.setText("Receive Progress");
   repaint();
}
};
t.start();

I also tried javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { /* update GUI code */ } });.

My question is, how do I call a method (that updates the GUI) within the GUI class from another thread? Keeping in mind that code is executed perfectly, the GUI elements are just not updated.

Any help would be greatly appreciated.


From your post

Whenever incomingFileRequest is called from within the GUI class (from any EventListener) it works perfectly and the GUI is updated.

However, when I call incomingFileRequest from the Server class/thread, the code runs and returns the correct value, but the GUI does not update.

That is exactly what some ppl tried to explain to you in the comments. All GUI updates must be performed in GUI thread - EDT.

The Event Dispatch Thread is the one that is handling your "listener" that is why GUI gets updated. To update your gui from the different thread you have to simply "schedule" your changes to be performed in the EDT. To do that, simply wrap your GUI modification code into Runnable class and pass it to the EDT via SwingUtilities.invokeLalater(Runnable) method. This way your GUI will be updated ASAP

You have also mentioned that

I also tried javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { /* update GUI code */ } });.

And what was the result? Because this is exactly how it should be done. If this is not working for you, there is some other porblem with your code either your method body waits for some external updates, or it is scheduled in different moment than you think it is.