Why does InvokeLater cause my JFrame not to display correctly?

六月ゝ 毕业季﹏ 提交于 2019-11-28 10:25:29
Carlos Heuberger

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.

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.

My answer was that when the GUI is constructed its not automatically painted at that time, instead a call to paint is placed in the EDT queue. If in the same method you construct a GUI object, and setVisible(true) then in the next few lines do something intensive, it blocks the future call to paint from occurring, because it will not get placed into the EDT queue until that method (with the intensive stuff) finishes. Also as was mentioned the frame or border is on the platform side of the equation (hence gets drawn), and the rest (Jlabel, container, background, etc) is on the java side and doesn't happen until paint is actually run (i.e. the EDT queue gets to it). My example code worked without the InvokeLater call, because that ran the intensive stuff in the init thread, and allowed the EDT thread to still paint.

Invisible components are not painted.

Oyvind Kvalnes

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!