Results 1 to 3 of 3
- 10-04-2010, 09:09 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 1
- Rep Power
- 0
Stopping a Timer from Inside the timer
Hi ,
I've hit upon an interesting problem . Here's my code
public class ToggleAnnotationsAction{
// Lots of things needed for the class
public void myMethod(ActionEvent e ){
Timer timer = new Timer(10, new ActionListener() {
private long panFrame = curFrame;
private long endFrame = approxFrame;
public void actionPerformed(ActionEvent evt) {
if(myDir == Direction.FORWARD){
if (panFrame >= endFrame) {
//Stop the Timer and exit
}
//Do something
}
else if(myDir == Direction.BACKWARD){
if (panFrame <= endFrame) {
// Stop the Timer and exit
}
}
}
} );
timer.start();
// This is where i want the control after the execution and return
return;
}
}
What i'm trying to do is stop my timer when a specific condition has been reached .
Any ideas of how this can be done ?
Thanks in advance
Krishnan
- 10-04-2010, 11:54 PM #2
Let me restate part of your question.
You want the thread that calls the timer.start() method to wait until the timer thread ends before continuing to the return
statement?
Then why put it on a timer? Just call it and await its return.
stop my timer when a specific condition has been reached .Last edited by Norm; 10-05-2010 at 12:04 AM.
-
I could see how you could want to stop the timer if this is say a simple animation. To get a handle on the Timer from within its ActionListener, you would call getSource() on the ActionEvent object passed into the actionPerformed method. For example:
Java Code:public void actionPerformed(ActionEvent evt) { if(myDir == Direction.FORWARD) { if (panFrame >= endFrame) { //Stop the Timer and exit Timer timer = (Timer)evt.getSource(); timer.stop(); return; } }
Similar Threads
-
Timer
By Learning Java in forum New To JavaReplies: 3Last Post: 08-11-2010, 01:18 PM -
Inside a Timer thread loop,how to refresh a JTable in swing
By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 02:45 AM -
Timer help
By Kinyo in forum New To JavaReplies: 15Last Post: 03-15-2009, 03:37 AM -
EJB Timer
By mrjunsy in forum New To JavaReplies: 0Last Post: 08-04-2008, 07:47 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 03:46 PM
Bookmarks