Solved

stop a Timer

Posted on 2004-10-18
13
227 Views
Last Modified: 2010-03-31
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
Comment
Question by:xenia27
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12337024
cancel()
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 12337026
updateTimer.stop();
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 12337031
Sorry: stop()

(cancel() is for java.util.Timer)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 92

Expert Comment

by:objects
ID: 12337034
you'll need to make your UpdateListener class an inner class of the one that defines updateTimer.
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 100 total points
ID: 12337038
Hi

updateTimer.stop()

0
 

Author Comment

by:xenia27
ID: 12337040
I guess, I need to make updateTimer be a global...^^"
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12337043
Or you can pass it to your actionlistener subclass as a parameter, and store it in a member variable
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12337048
class UpdateListener implements ActionListener
{
    Timer timer;
    public UpdateListener(Timer timer)
    {this.timer=timer;}
...
0
 
LVL 92

Expert Comment

by:objects
ID: 12337050
> 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
 
LVL 13

Expert Comment

by:Webstorm
ID: 12337060
>> 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
 

Author Comment

by:xenia27
ID: 12337069
OK...got this work perfectly~  Thanks~~~~
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12337073
:-) glad your problem is solved
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12337273
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 87
ForLoop Example 3 49
Arrays.asList  VS  ArrayList 4 62
how to add new optional parameter to JSP 1 43
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question