Quantcast

java.awt.EventQueue.invokeLater().

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

java.awt.EventQueue.invokeLater().

Bill Slack
I am trying to figure out why Netbeans uses java.awt.EventQue.invokeLater(), when all of the java swing tutorials use javax.swing.SwingUtilites.invokeLater().  Could someone explain the difference?
 
I suspect the answer is in the age of the tutorials and the following statement taken from javax.swing.SwingUtilities: As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater()."
 
but what does "...this method is just a cover..." mean?
 
Thanks,
Bill 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: java.awt.EventQueue.invokeLater().

Wade Chandler-5
Administrator
--- Bill Slack <[hidden email]> wrote:

> I am trying to figure out why Netbeans uses
> java.awt.EventQue.invokeLater(), when all of the
> java swing tutorials use
> javax.swing.SwingUtilites.invokeLater().  Could
> someone explain the difference?
>
> I suspect the answer is in the age of the tutorials
> and the following statement taken from
> javax.swing.SwingUtilities: As of 1.3 this method is
> just a cover for java.awt.EventQueue.invokeLater()."
>
> but what does "...this method is just a cover..."
> mean?
>
> Thanks,
> Bill

It means it merely turns around and calls the other
method, and SwingUtilities.invokeLater is kept for
backward compatibility to keep from breaking older
code.  The JDK source code is freely available, and is
really a good way to answer some questions quickly.  I
recommend everyone keep a copy handy while programming
Java as it's a big help.  This directly from the
source code:
    /**
     * Causes <i>doRun.run()</i> to be executed
asynchronously on the
     * AWT event dispatching thread.  This will happen
after all
     * pending AWT events have been processed.  This
method should
     * be used when an application thread needs to
update the GUI.
     * In the following example the
<code>invokeLater</code> call queues
     * the <code>Runnable</code> object
<code>doHelloWorld</code>
     * on the event dispatching thread and
     * then prints a message.
     * <pre>
     * Runnable doHelloWorld = new Runnable() {
     *     public void run() {
     *         System.out.println("Hello World on " +
Thread.currentThread());
     *     }
     * };
     *
     * SwingUtilities.invokeLater(doHelloWorld);
     * System.out.println("This might well be
displayed before the other message.");
     * </pre>
     * If invokeLater is called from the event
dispatching thread --
     * for example, from a JButton's ActionListener --
the <i>doRun.run()</i> will
     * still be deferred until all pending events have
been processed.
     * Note that if the <i>doRun.run()</i> throws an
uncaught exception
     * the event dispatching thread will unwind (not
the current thread).
     * <p>
     * Additional documentation and examples for this
method can be
     * found in
     * <A
HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
to Use Threads</a>,
     * in <em>The Java Tutorial</em>.
     * <p>
     * As of 1.3 this method is just a cover for
<code>java.awt.EventQueue.invokeLater()</code>.
     *
     * @see #invokeAndWait
     */
    public static void invokeLater(Runnable doRun) {
        EventQueue.invokeLater(doRun);
    }



Wade
Loading...