Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To understand what <code>invokeAndWait()</code> does, you first need to understand the event/thread model of Swing.</p> <p>Basically, <strong>everything that affects the GUI in any way must happen on a single thread</strong>. This is because experience shows that a multi-threaded GUI is impossible to get right. </p> <p><strong>In Swing, this special GUI thread is called the Event Dispatch Thread, or EDT</strong>. It is started as soon as a Swing top-level component is displayed, and it's <strong>bascially a worker thread that has a FIFO queue of event objects that it executes one after another.</strong></p> <p>When a Swing GUI needs to be drawn or updated, the JRE places an event on the EDT queue. User actions that cause listeners to be called start as events on the EDT queue. And (this is this is the important part) everything your program does that changes the GUI (like registering listeners, adding/removing GUI components or changing model data that the GUI displays) must be placed in the EDT queue, or the GUI can get corrupted.</p> <p>And now for the finish: <strong><code>invokeAndWait()</code> places the <code>Runnable</code> you pass to it into the EDT event queue and waits until the EDT has executed it.</strong> This should be used when a non-GUI thread needs to do something that affects the GUI, but also needs to wait until it is actually done before it can continue. If you just want to do something that affects the GUI but do not care when it is finished, you should instead use <code>invokeLater()</code>.</p>
 

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