Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does InvokeLater cause my JFrame not to display correctly?

Ok I've read an searched all over the web, and I've not found the solution to my problem yet, perhaps I'm missing something simple, hence here I am...

I've got a rather large project, that handles work orders for a repair business. It's all database connected, many many pages of code, and classes. But i just added a short bit of code to the front end that essentially checks for new messages in our notes area.

Anyway, I display a simple JFrame with two JLabels while a separate thread queries the database. This all happens at the start of the program. The problem is my little "please wait" JFrame comes up with its frame but no guts, no background, and no JLabels, during the wait (which is the rest of the program loading, not the database thread), afterwords it displays, but by then its missing its point.

I wrote the following example program. It displays a simple JFrame (CheckingMessagesGUI: a JFrame with two JLabels, nothing more) sleeps for 5 sec then displays the Example (main program) JFrame, then instantly closes (System.exit(0)) in this example, of course my real program goes on to do a lot more. What I found is that invokeLater seems to be causing the problem. Once the sleep timer runs out the window will display, but the code to display it was given before the Thread.sleep command, and should have been done in that order correct?

My question is why does invokeLater cause my JFrame not to display correctly?

Its my understanding that the purpose of invokeLater is so that the items run on the correct AWT event thread, which would make me think that this window would get painted correctly. Anyway I'm sure I'm missing something obvious. I commented out the invokeLater part in the code below, and it runs correctly, if you put it back it doesn't...

Many thanks in advance.

package javaapplication6;

public class Example extends javax.swing.JFrame {          
    public Example() {
        System.out.println("Example started");
        setBounds(100,100,200,200);

        System.out.println("cmGUI instantiated");
        CheckingMessagesGUI cmGUI = new CheckingMessagesGUI();
        System.out.println("Set cmGUI visible");
        cmGUI.setVisible(true);
        cmGUI.validate();
        try {
            System.out.println("timer started");
            Thread.sleep(5000);
            System.out.println("timer done");
        } catch(InterruptedException e){
        }
        System.exit(0);
    }

    public static void main(String[] args) {
        /*java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() { */
        System.out.println("Started");
        System.out.println("example Instantiated");
        Example example = new Example();
        System.out.println("example visible");
        example.setVisible(true);
        /*      }
        });
        */
    }
}

UPDATE: To clarify, I realize Thread.sleep() will block everything, but shouldn't my CheckingMessagesGUI already have been fully drawn before I call sleep? That is the issue.

like image 997
Marc Avatar asked Dec 03 '09 23:12

Marc


3 Answers

invokeLater runs the Runnable in the Event Dispatch Thread which also is used for updating the GUI.
Your sleep is blocking this Thread so the GUI also does not get serviced, no updates can be done till you return from the invokeLater code.
That's why you should not do any long (time consuming) computations in this Thread. They should be done in an different (new) Thread.

The Event Dispatch Queue states

Tasks on the event dispatch thread must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive.

Your code could be changed to (not tested):

public Example(){
    System.out.println("Example started");
    setBounds(100,100,200,200);

    System.out.println("cmGUI instantiated");
    CheckingMessagesGUI cmGUI = new CheckingMessagesGUI();
    System.out.println("Set cmGUI visible");
    cmGUI.setVisible(true);
    cmGUI.validate();

    Thread thread = new Thread(new Runnable() {
        try {
            System.out.println("timer started");
            Thread.sleep(5000);
            System.out.println("timer done");
        } catch(InterruptedException e) {
        }
        System.exit(0);
    });
    thread.start();
}

EDIT: let's go a bit "deeper" (and it's my view of the working of Swing/AWT).
I suppose the "please wait" (see comments) should be displayed in the CheckingMessagesGUI class, but isn't.
That's related to the way the GUI works. It does not directly change anything on the display if you call the corresponding (Swing) methods (draw, setText, setLocation, ...); it just queues an Event in the Event Queue. The Event Dispatch Thread is (should be) the only Thread that reads this queue and process the events. As long as it is being blocked - by the sleep in this case - no changes to the GUI will be displayed. The GUI is frozen.

EDIT2:
invokeLater the Runnable is appended to the end of the queue to be latter executed by the EDT after all pending events have been processed, the next command after the invokeLater call will be executed.
invokeAndWait same as above but the actual Thread blocks until the Runnable was executed (after pending events) by the EDT, that is, the command following the invokeAndWait will only get started after the submitted Runnable was executed.

like image 89
user85421 Avatar answered Oct 22 '22 04:10

user85421


my understanding that the purpose of InvokeLater is so that the items run on the correct AWT event thread

That is correct.

However, that also means that Thread.sleep() is executing on the EDT, which means the GUI can't repaint itself, since you just told the EDT to sleep. You need to use a separate Thread for your long running task.

Read the section from the Swing tutorial on Concurrency for more information about the EDT.

but my point was that my "please wait" (CheckingMessagesGUI) should have already been drawn fully before i called sleep. Shouldn't this be true?

Here is my simplified understanding of the process. The frame is created and displayed because it is an OS native component. However, the contentPane and child component are lightweight components which means the Swing Repaint Manager schedules when they should be repainted. So before the repainting is scheduled, the EDT is put to sleep, and the repainting can't be done until the sleeping is finished.

You can find more infomation about the Repaint Manager in the article on Paintng in AWT and Swing.

like image 40
camickr Avatar answered Oct 22 '22 06:10

camickr


Here is a general solution for newbies like myself who have problems finding what they need in the Swing tutorial.

public void method(){
    final PleaseWaitWindow window = new PleaseWaitWindow();

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            //stuff that you want to do that is preventing window to display

            window.dispose();
        }
    }
    thread.start();
}
like image 33
Oyvind Kvalnes Avatar answered Oct 22 '22 04:10

Oyvind Kvalnes