Link to home
Start Free TrialLog in
Avatar of xenia27
xenia27Flag for Taiwan, Province of China

asked on

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
Avatar of zzynx
zzynx
Flag of Belgium image

cancel()
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you'll need to make your UpdateListener class an inner class of the one that defines updateTimer.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xenia27

ASKER

I guess, I need to make updateTimer be a global...^^"
Avatar of Webstorm
Webstorm

Or you can pass it to your actionlistener subclass as a parameter, and store it in a member variable
class UpdateListener implements ActionListener
{
    Timer timer;
    public UpdateListener(Timer timer)
    {this.timer=timer;}
...
> 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.
>> 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.
Avatar of xenia27

ASKER

OK...got this work perfectly~  Thanks~~~~
:-) glad your problem is solved
Thanks