About event mechanisms in Java swing

Source: Internet
Author: User

You have seen two ways to start the code for the main form:

Mode 1:

        Java.awt.EventQueue.invokeLater (New Runnable () {public            void run () {                new Mainjframe (). setvisible (true);        });

Mode 2

        Javax.swing.SwingUtilities.invokeLater (New Runnable () {@Overridepublic void Run () {new Mainjframe (). setvisible (True) ;}});

Swingutilities.invokelater and Eventqueue.invokelater are actually the same. Swingutilities.invokelater is actually called Eventqueue.invokelater.

JDK Source code

    public static void Invokelater (Runnable dorun) {        eventqueue.invokelater (dorun);    }

Swingutilities.invokelater's Javadoc has such an explanation:


Open Declaration void Javax.swing.SwingUtilities.invokeLater (Runnable dorun)

Causes Dorun.run () to is executed asynchronously on the AWT event dispatching thread (that is, EDT). This would happen after all pending AWT events has been processed. This method should is used when a application thread needs to update the GUI. In the following example the Invokelater call queues the Runnable object Dohelloworld on the event dispatching thread and Then prints a message.

Runnable Dohelloworld = new Runnable () {public void run () {System.out.println ("Hello World on" + Thread.currentthre  AD ()); }}; Swingutilities.invokelater (Dohelloworld); System.out.println ("This might well is displayed before the other message.");

If Invokelater is called from the event dispatching thread--for example, from a JButton ' s ActionListener--the dorun.ru N () would still is deferred until all pending events has been processed. Note if the Dorun.run () throws an uncaught exception the event dispatching thread would unwind (not the current thread ).
Additional documentation and examples for the This method can is found in Concurrency in Swing.

As of 1.3 This method was just a cover for Java.awt.EventQueue.invokeLater ().

Unlike the rest of Swing, this method can is invoked from any thread.
Parameters:dorun See also:invokeandwait

Event Dispatching thread is often said EDT, Event dispatch Threads

For the mechanism of event distribution, see the following article, which is more comprehensive.

http://blog.itpub.net/13685345/viewspace-374940/

About event mechanisms in Java swing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.