Event Processing for Swing component sets (5)

Source: Internet
Author: User

2.2.4 Timer class
In addition to the invokeAndWait () and invokeLater () Methods of EventQueue, we can also use the Timer class to create actions executed on the event distribution thread. Timer provides a method to notify ActionListener after a predefined time. The timer can repeatedly notify the listener, or only notify once.

Create a timer object

The following is a constructor used to create a Timer with a specified millisecond latency between ActionListener calls:

Public Timer (int delay, ActionListener actionListener );
// 1 second interval
Timer timer = new Timer (1000, anActionListener); Use Timer object

After the Timer object is created, start () must be started (). Once Timer is started, ActionListener will be notified after the specified time. If the system is busy, the latency will be longer, but it will never be shorter.

If we need to stop Timer, we can call the stop () method. Timer also has a restart () method, which calls stop () and start () to restart the delay interval.

To demonstrate the needs, list 2-8 defines an ActionListener that only outputs a simple message. Then we create a Timer to call this listener every half second. After we create a timer, we need to start it.

/**
*
*/
Package swingstudy. ch02;
 
Import java. awt. EventQueue;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
 
Import javax. swing. Timer;
 
/**
* @ Author lenovo
*
*/
Public class TimerSample {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
 
Runnable runner = new Runnable (){
Public void run (){
ActionListener actionListener = new ActionListener (){
Public void actionreceivmed (ActionEvent event ){
System. out. println ("Hello world timer ");
}
};
 
Timer timer = new Timer (500, actionListener );
Timer. start ();
}
};
 
EventQueue. invokeLater (runner );
}
 
} Timer attributes

Table 2-1 lists the six attributes of Timer. Four actions that allow us to customize the timer. Running tells us whether the timer is started but not stopped, and actionListeners will provide us with a list of action listeners.

Attribute name
Data Type
Accessibility
 
ActionListeners
ActionListener []
Read-Only
 
Coalesce
Boolean
Read/write
 
Delay
Int
Read/write
 
InitialDelay
Int
Read/write
 
Repeats
Boolean
Read/write
 
Running
Boolean
Read-Only
 

The delay attribute is the same as that of the constructor. If we change the latency of a running timer, the new latency will be used only when the existing latency times out.

The initialDelay attribute allows us to initiate latency other than the interval delay after the first operation. For example, if we do not want to execute a task in the previous hour, but we want to execute it every 15 minutes, we need to modify the initialDelay setting before starting the timer. By default, initialDelay and delay attributes are set to the same settings in the constructor.

The repeats attribute is set to true by default to run the timer repeatedly. When set to false, the timer only notifies the action listener once. However, we need to restart the restart () monitor to trigger the listener again. A non-repeating timer can be used for a notification that occurs after an event is triggered.

The coalesce attribute allows a busy system to Discard unused notifications when a registered ActionListener object has a new event to be triggered. By default, the coalesce value is set to true. This means that if a timer runs once every 500 milliseconds, but the system is very busy and has not responded for 2 seconds, the timer only needs to send one message instead of sending the lost message. If this attribute is set to false, four messages need to be sent.

In addition to the listed attributes, we can also use the following code to allow log messages:

Timer. setLogTimers (true); log messages are very useful for actions without visualized elements, so that we know the occurrence of events.

Related Article

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.