stop a Timer

Hi,

I wanna know how can I stop a timer from the timer's ActionListener??

here is my codes...

javax.swing.Timer updateTimer = new javax.swing.Timer(500, new UpdateListener());

class UpdateListener implements ActionListener
{
  public void actionPerformed(ActionEvent ae)
  {
    if ( Condition == true)
      // I want to stop updateTimer here, how can I do so????
  }
}




Xenia
xenia27Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
updateTimer.stop();
0
 
zzynxSoftware engineerCommented:
cancel()
0
 
zzynxConnect With a Mentor Software engineerCommented:
Sorry: stop()

(cancel() is for java.util.Timer)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
you'll need to make your UpdateListener class an inner class of the one that defines updateTimer.
0
 
WebstormConnect With a Mentor Commented:
Hi

updateTimer.stop()

0
 
xenia27Author Commented:
I guess, I need to make updateTimer be a global...^^"
0
 
WebstormCommented:
Or you can pass it to your actionlistener subclass as a parameter, and store it in a member variable
0
 
WebstormCommented:
class UpdateListener implements ActionListener
{
    Timer timer;
    public UpdateListener(Timer timer)
    {this.timer=timer;}
...
0
 
objectsCommented:
> I guess, I need to make updateTimer be a global...^^"

not if you make your UpdateListener class an inner class of the one that defines updateTimer.
0
 
WebstormCommented:
>> not if you make your UpdateListener class an inner class of the one that defines updateTimer.

and if the UpdateListener class is a static inner class, updateTimer must also be static.
0
 
xenia27Author Commented:
OK...got this work perfectly~  Thanks~~~~
0
 
WebstormCommented:
:-) glad your problem is solved
0
 
zzynxSoftware engineerCommented:
Thanks
0
All Courses

From novice to tech pro — start learning today.