Introduction to invokeLater and invokeAndWait in SwingUtilities

Source: Internet
Author: User
Tags event listener

In Java, Swing is thread-insecure and is a single-threaded design. As a result, only the event dispatching thread can access the Swing component to be drawn on the screen. The event dispatching thread is the thread that calls callback methods such as paint and update. It is also the event processing method defined in the event listener interface. For example, the actionreceivmed method in the ActionListener is called in the event dispatching thread.
Swing is event-driven, so it is natural to update the visible GUI in the callback function. For example, if a button is pressed and the project list needs to be updated, the list is updated by the actionreceivmed method of the event listener associated with the button. It is not normal to update the Swing component from a thread other than the event dispatching thread.
Sometimes it is necessary to update the Swing component from a thread other than the event dispatching thread. For example, there is a very time-consuming operation in actionreceivmed and it takes a long time to return the result, after the button is activated, it takes a long time to see the updated list. The Button remains pressed for a long time and returns only to actionreceivmed. Generally, time-consuming operations should not be executed in the event processing method, before an event is returned, other events cannot be triggered, and the interface is similar to a stuck state. Therefore, it may be better to execute time-consuming operations on an independent thread, this will immediately update the user interface and release the event dispatching thread to dispatch other events.
The SwingUtilities class provides two methods: invokeLate and invoteAndWait, which both queue the runable objects on the event dispatching thread. The run method is called when the running object ranks first in the event dispatch queue. The effect is to allow the event dispatching thread to call any code block in another thread.
The component can be updated only from the event dispatching thread.
Program example: update component error method
StartButton. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
GetInfoThread t = new GetInfoThread (Test. this );
T. start ();
StartButton. setEnabled (false );
   }
});
  
Class GetInfoThread extends Thread {
Test applet;

Public GetInfoThread (Test applet ){
This. applet = applet;
 }

Public void run (){
While (true ){
Try {
Thread. sleep (500 );
Applet. getProgressBar (). setValue (Math. random () * 100 );
} Catch (InterruptedException e ){
E. printStackTrace ();
    }
   }
  }
 }
Error analysis: In actionreceivmed, the listener sets the allowed status of the button to false. Because actionreceivmed is called on the event dispatching thread, setEnabled is a valid operation, however, it is dangerous to set the progress bar in GetInfoThread because the threads other than the event dispatching thread update the progress bar, so the operation is abnormal.

1. Use invokeLater
Class GetInfoThread extends Thread {
Test applet;
 
Runnable runx;
 
Int value;</

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.