Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does InvokeLater cause my JFrame not to display correctly?
    primarykey
    data
    text
    <p>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...</p> <p>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. </p> <p>Anyway, I display a simple <em>JFrame</em> with two <em>JLabel</em>s while a separate thread queries the database. This all happens at the start of the program. The problem is my little "please wait" <em>JFrame</em> comes up with its frame but no guts, no background, and no <em>JLabel</em>s, 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. </p> <p>I wrote the following example program. It displays a simple <em>JFrame</em> (CheckingMessagesGUI: a <em>JFrame</em> with two <em>JLabel</em>s, nothing more) sleeps for 5 sec then displays the Example (main program) <em>JFrame</em>, then instantly closes (<code>System.exit(0)</code>) in this example, of course my real program goes on to do a lot more. What I found is that <code>invokeLater</code> 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 <code>Thread.sleep</code> command, and should have been done in that order correct?</p> <p>My question is why does <code>invokeLater</code> cause my <em>JFrame</em> not to display correctly? </p> <p>Its my understanding that the purpose of <code>invokeLater</code> 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 <code>invokeLater</code> part in the code below, and it runs correctly, if you put it back it doesn't...</p> <p>Many thanks in advance.</p> <pre><code>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); /* } }); */ } } </code></pre> <p>UPDATE: To clarify, I realize <code>Thread.sleep()</code> will block everything, but shouldn't my CheckingMessagesGUI already have been fully drawn before I call sleep? That is the issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload