This Program will work fine...
setRepeats(boolean flag)
function used to set call the function(actionPerformed)
repeatedly or only one time if
timer.setRepeats(false) == timer
calls the actionperformed method for only one time
timer.setRepeats(true) == timer
calls the actionPerformed method repeatedly based on specified time
Swing Timer Work
- do the task one time
- do the task repeated time
steps to create swing timer:
- create the actionlistener
- create the timer constructor then pass time and actionlistener in that
- implement the
actionPerformed()
function in which do your task
- use
timer.start()
for start the task between the time specified in timer constructor, use timer.stop()
for stop the task
Example:
ActionListener al=new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//do your task
if(work done)
timer.stop();//stop the task after do the work
}
};
Timer timer=new Timer(1000,al);//create the timer which calls the actionperformed method for every 1000 millisecond(1 second=1000 millisecond)
timer.start();//start the task