Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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
0
xenia27
Asked:
xenia27
  • 5
  • 3
  • 3
  • +1
3 Solutions
 
zzynxSoftware engineerCommented:
cancel()
0
 
objectsCommented:
updateTimer.stop();
0
 
zzynxSoftware engineerCommented:
Sorry: stop()

(cancel() is for java.util.Timer)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
objectsCommented:
you'll need to make your UpdateListener class an inner class of the one that defines updateTimer.
0
 
WebstormCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now