![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Using invokeLater and invokeAndWaitThere 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 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 applications 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.
TimersJFC 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 threadyou dont 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 ManagerAll 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.
|
![]() |
|