Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Using invokeLater and invokeAndWait

There are situations where you will need to access Swing user interface components on threads other than the event-dispatch thread. The following list gives some examples of such situations.

  When you must perform a long initialization sequence at startup time.
The initialization cannot occur in the event-dispatching thread because repainting and event dispatching would be blocked for the duration of the initialization. However, the initialization code does need to update GUI components to indicate the progress of the initialization.
  When you must update the user interface as the result of a non-AWT event.
Programs, such as servers that can receive requests from other programs, may need to update GUI components in response to external requests that are not executing in the event-dispatch thread.

When you need to access Swing component methods that are not guaranteed to be thread safe from threads other than the event-dispatch thread, you can use the invokeLater and invokeAndWait methods in the SwingUtilities class. These methods allow code to be run on the event-dispatch thread. SwingUtilities also provides the isEventDispatchThread to allow you to determine at runtime if code is running in the event-dispatch thread.

public static void invokeLater(Runnable code);
public static boolean isEventDispatchThread();
public static void invokeAndWait(Runnable code);

The invokeLater and invokeAndWait methods take a Runnable object and execute it on the application’s event-dispatch thread. The following fragment shows how you can use an anonymous inner class as a Runnable object to contain code to be executed by invokeLater.

Runnable Update = new Runnable() {
    public void run() {
        // Put your code here
    }
};
SwingUtilities.invokeLater(Update);

Note that the Runnable interface contains a single method, run. The code in the run method is the code that is executed when the runnable object is invoked.


Note:  
The invokeAndWait method is similar to the invokeLater method except that it waits until the Runnable object is executed before returning. You can use invokeAndWait the same way you use invokeLater to ensure that your access of Swing components is thread safe.

Timers

JFC timers utilize timer listeners to serve as callbacks for periodic or one-shot timer objects. All timer listeners are executed in a single system thread to reduce the usage of system resources.

The system timer thread executes timer action events in the event-dispatch thread—you don’t need to use invokeLater and invokeAndWait to access Swing components in timer listeners. A complete description of timers in the JFC is presented in Chapter 31, “Timers.”

The Repaint Manager

All Swing components and any custom components based on JComponent utilize a repaint manager to manage screen repaint and double buffering operations. The repaint manager keeps track of invalid components and dirty regions and batches these operations so that they can be processed simultaneously on the event-dispatch thread. Repaint operations are done through the use of an offscreen buffer to ensure smooth repaints. JFC maintains one repaint manager instance per thread group.

Any JFC application can take advantage of the services of the repaint manager simply by using the component repaint and revalidate methods. The only reason for most applications to directly access the repaint manager is to disable double buffering for performance, debugging, or other reasons. However, the repaint manager is designed to be extensible for applications that want to plug in their own repaint batching or double buffering solutions.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.