Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 481001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:57:01+00:00 2026-05-13T00:57:01+00:00

Ok I’ve read an searched all over the web, and I’ve not found the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:57:01+00:00Added an answer on May 13, 2026 at 12:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.