Java notes--Update swing controls with event allocation threads

Source: Internet
Author: User

To update the swing control with the event allocation thread:

Swing is not thread-safe, and if you update the swing control in multiple threads, it is likely to cause the program to crash.
To avoid this problem, you can use time-allocation threads to update the swing control.

EventQueue is a platform-independent class that queues events from the underlying peer class and trusted application classes.
It encapsulates the asynchronous event assignment mechanism, which extracts events from the queue and then invokes Dispatchevent (awtevent) on EventQueue
method to assign these events (the event is assigned as a parameter). The special behavior of the mechanism is related to implementation. In order to assign the swing program to the event
Thread, you need to use the Invokelater () method:
Public static void Invokelater (Runnable Runnable)
This run method should be executed synchronously on the EventQueue


Note: The event allocation thread never needs to use a potentially blocking operation, such as an IO operation
--If you want to reprint this article please indicate the reprint address "http://www.cnblogs.com/XHJT/p/3911806.html" Thank you--
code example:

 PackageCom.xhj.thread;ImportJava.awt.Button;ImportJava.awt.EventQueue;ImportJava.awt.Label;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.util.Random;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;/*** Update Swing control with event allocation thread * *@authorXiehejun **/ Public classeventthreadupdateswing {//Number of identity updates    Private intA = 0; PrivateLabel lable; Privatebutton button;  Public voidsetlable (Label lable) { This. lable =lable; }     Public voidSetbutton (Button button) { This. Button =button; }     PublicLabel getlable () {returnlable; }     PublicButton Getbutton () {returnbutton; }    /*** Event Thread class*/    Private classEnventthreadImplementsRunnable {@Override Public voidrun () {//button monitoring to achieve click Change ValueButton.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//using the Enventqueue class to update the swing controlEventqueue.invokelater (NewRunnable () {@Override Public voidrun () {Lable.settext (a+ + + "The newly generated random number is:" +NewRandom (). Nextint ());                }                    });        }            }); }    }     Public Static voidMain (string[] args) {eventthreadupdateswing ETUs=Neweventthreadupdateswing (); JFrame Frame=NewJFrame (); JPanel Panel=NewJPanel ();        Frame.setcontentpane (panel); Frame.setvisible (true); Etus.setlable (NewLabel ("click button to generate random number"))); Etus.setbutton (NewButton ("Start Build"));        Frame.add (Etus.getlable ());                Frame.add (Etus.getbutton ()); Enventthread envent= ETUs.NewEnventthread (); NewThread (envent). Start (); }}

Note: a invokeandwait () method is also defined in EventQueue, and the Invokelater () method returns the result immediately when the event is put into the queue.
The run () method executes in the new thread. The Invokeandwait method does not return until the run () method is executed. The Swingutilities also offers
The same method.

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.