Java Quiz Player

Blog Post

SwingUtilities.invokeAndWait usage Example

May 14, 2015

1. Swing Threads

GUI (Graphical User Interface) applications are inherently asynchronous. A user may select a menu item or press a button at any time, and expects that the application will respond promptly. Java Swing and AWT address this issue by creating a separate thread for handling user initiated events and updating the graphical view presented to the user.

The event handling code runs on a special thread known as the event dispatch thread (EDT). Most code that invokes Swing methods also runs on this thread. Most tasks are invocations of event-handling methods, such as ActionListener.actionPerformed. Tasks on the event dispatch thread must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive or freezes.

Other tasks can be scheduled by the application code, using the static methods javax.swing.SwingUtilities.invokeLater or invokeAndWait.

To determine whether the code is running on the event dispatch thread, invoke the static method SwingUtilities.isEventDispatchThread.

2. The Example

This post shows how to use GUI actions (asynchronous) and non-GUI (synchronous) process together to get the desired user experience using the EDT and the application threads. The example uses the invokeAndWait.

The example app is a Swing GUI dialog where a user starts a process and waits for it to complete. The GUI has a status message area and buttons to start or interrupt the process and close the app.

The app is a XYZ Database Restore program. The app supposes that a backup copy of the database is stored in a directory. The user selects the directory and starts the restore process by clicking Restore Database button. Once the process in initiated all the buttons are disabled (except the interrupt) and the user waits a few seconds until the process completes. The process completes - a status is displayed and the buttons are enabled for the user to proceed further.

GUI image

When the Restore Database button is clicked, the buttons are to be disabled and a process start status message is to be displayed (as seen in the above screenshot). After these GUI actions complete, the restore process should start. These two sets of actions cannot be run from the EDT, but from an application thread.

The GUI actions are run in an EDT using the invokeAndWait, and the restore process runs within the application thread. Both these together run from an application thread started from the actionPerformed method of Restore Database button's action listener (of type java.awt.event.ActionListener). Note that the action listener's action performed method code runs in an EDT.

The following are the details:

The user proceeds further with next actions - like close the dialog as the database restore is completed or work with any errors as shown in the status display.

3. The Code

The following code snippet shows the Restore Database button's action listener's action performed method (there is a link to download the complete program code at the bottom of this post):

 1:  public void actionPerformed(ActionEvent e) {
 3:      System.out.println("actionPerformed isEventDispatchThread: " +
 4:          SwingUtilities.isEventDispatchThread()); // this returns true
 6:      final Runnable runInitially = new Runnable() {
 8:          public void run() {
10:             // These GUI actions need to be completed before
11:             // the doDbRestore()
12:             restoreButton.setEnabled(false);
13:             closeButton.setEnabled(false);
14:             browseButton.setEnabled(false);
15:             interruptButton.setEnabled(true);
16:             messageArea.append(RESTORE_START + NEWLINE);
17:             System.out.println("invokeAndWait isEventDispatchThread: " +
18:                 SwingUtilities.isEventDispatchThread()); // this returns true
19:          }
20:      };
22:      // This code is to run in an non event dispatch thread.
23:      // For that, this is run using the SwingUtilities.invokeAndWait().
24:      Thread appThread = new Thread() {
26:          public void run() {
28:              System.out.println("appThread isEventDispatchThread: " +
29:                  SwingUtilities.isEventDispatchThread()); // this returns false
31:              try {
32:                  SwingUtilities.invokeAndWait(runInitially);
33:              }
34:              catch (InterruptedException | InvocationTargetException ex) {					
35:                  ex.printStackTrace();
36:             }
38:             // NOTE: Runs in a separate thread where the current thread waits
39:             // until the restore process completes in different thread.
40:             // See doDbRestore() for details.		
41:             boolean restoreStatus = doDbRestore(); 
43:             if (restoreStatus) {
44:                  messageArea.append(RESTORE_SUCCESS + NEWLINE);
45:             }
46:             else {
47:                  messageArea.append(RESTORE_FAILURE + NEWLINE);
48:             }
50:             browseButton.setEnabled(true);
52:             closeButton.setEnabled(true);
53:             interruptButton.setEnabled(false);
54:          }
55:      }; // Thread
57:      appThread.start();
58:  }

4. The Output

The following is the output on the console after the database restore is completed. Verify the type of thread at each stage of the process from the point of clicking the Restore Database button. Note the above code snippet's lines 3, 28 and 17 print the following output:

actionPerformed isEventDispatchThread: true
appThread isEventDispatchThread: false
invokeAndWait isEventDispatchThread: true

Note the invokeAndWait method should be used only when an application thread needs to update the GUI; it shouldn't be called from the event dispatching thread.

5. Download

Download source code here:

NOTE: Java SE 7 is required to compile and run the code.

6. Useful Links

Return to top


Comments are welcome. Please note that comments are moderated.
Email to: info(at)javaquizplayer(dot)com.