Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...