Results 1 to 3 of 3
  1. #1
    krishnan is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    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 .
    What happens if you call stop for the timer and exit? Have you tried it?
    Last edited by Norm; 10-04-2010 at 11:04 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Timer
    By Learning Java in forum New To Java
    Replies: 3
    Last Post: 08-11-2010, 12:18 PM
  2. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  3. Timer help
    By Kinyo in forum New To Java
    Replies: 15
    Last Post: 03-15-2009, 02:37 AM
  4. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 06:47 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •