Event dispatching thread: Difference between revisions

Content deleted Content added
No edit summary
Line 5:
Other application threads can execute code in the event dispatch 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 dispatch 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>[http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/SwingWorker.java [SwingWorker]]</code> class, developed by [[Sun Microsystems]], is an implementatino 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>.
 
== See also ==