Event dispatching thread: Difference between revisions

Content deleted Content added
No edit summary
m Executing code in the EDT: Add missing space
Line 3:
== Executing code in the EDT ==
 
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. Two methods of this class allow synchronous ({{Javadoc:SE|member=invokeAndWait(Runnable)|javax/swing|SwingUtilities|invokeAndWait(java.lang.Runnable)}}) and asynchronous ({{Javadoc:SE|member=invokeLater(Runnable)|javax/swing|SwingUtilities|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()}} 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 pattern|worker design pattern]]''. The <code>[[SwingWorker]]</code> class, developed by [[Sun Microsystems]], is an implementation of the worker design pattern, but is not a part of standard Swing distribution. The open source project [http://foxtrot.sourceforge.net/ Foxtrot] provides another synchronous execution solution similar to <code>SwingWorker</code>.