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.