Event dispatching thread: Difference between revisions

Content deleted Content added
Line 5:
Other application threads can execute code in the event dispatching thread by defining the code in a {{Javadoc:SE|java/lang|Runnable}} object and pass it to the {{Javadoc:SE|javax/swing|SwingUtilities}} helper class or to the {{Javadoc:SE|java/awt|EventQueue}}. Two methods of these classes allow synchronous ({{Javadoc:SE|member=invokeAndWait(Runnable)|javax/swing|SwingUtilities|invokeAndWait(java.lang.Runnable)}} or {{Javadoc:SE|member=invokeAndWait(Runnable)|java/awt|EventQueue|invokeAndWait(java.lang.Runnable)}}) and asynchronous ({{Javadoc:SE|member=invokeLater(Runnable)|javax/swing|SwingUtilities|invokeLater(java.lang.Runnable)}} or {{Javadoc:SE|member=invokeLater(Runnable)|java/awt|EventQueue|invokeLater(java.lang.Runnable)}}) code execution from the EDT. The method <code>invokeAndWait()</code> should never be called from the event dispatching thread&mdash;it will throw an [[Exception handling|exception]]. The method {{Javadoc:SE|javax/swing|SwingUtilities|isEventDispatchThread()}} or {{Javadoc:SE|java/awt|EventQueue|isEventDispatchThread()}} can be called to determine if the current thread is the event dispatching thread.
 
Another solution for executing code in the EDT is using the ''[[Worker patternSwingWorker|worker design pattern]]''. The <code>[[SwingWorker]]</code> class, developed by [[Sun Microsystems]], is an implementation of the worker design pattern, and as Java 6 is part of standard Swing distribution. The open source project [http://foxtrot.sourceforge.net/ Foxtrot] provides another synchronous execution solution similar to <code>SwingWorker</code>.
 
== See also ==