Results 1 to 5 of 5

Thread: stopping timer

  1. #1
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default stopping timer

    Hi,
    In the following code i can move the car object when pressing the button, but the problem is that i am unable to stop the car before it exits the panel.
    can any one help me??


    Java Code:
    final ActionListener animation = new ActionListener(){
    int i = 1; 
    public void actionPerformed(ActionEvent e){
    {int j= i++;
    car.setBounds(2+j,260,50,50);
    
    System.out.println(j);}
    
    }
    };
    
    ActionListener buttonAction = new ActionListener(){
    
    public void actionPerformed(ActionEvent e){
    
    final Timer timer = new Timer(10,animation);
    
    }
    };
    thx.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: stopping timer

    I really hope your code isn't actually formed like that. Some better indentation will make it much easier to understand. If you declare the timer in a larger scope you can reference it from other methods and call timer.stop().

  3. #3
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default Re: stopping timer

    Dear Dunde887
    Thanks for your reply and can you give me a more detailed explanation or if you can indicate a link to a similair problem.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: stopping timer

    What he is saying is that if you declare your time in an accessible place with an accessible scope, you can access it from anywhere (with the help of an accessor method if needed) in your program. Once you can 'see' it, you can call it's methods.

    In this way, other actionHandlers or classes can ask the main class for access to the timer, and then call timer.stop() on said timer.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. JOptionPane stopping timer
    By a_programmer in forum AWT / Swing
    Replies: 8
    Last Post: 08-09-2011, 04:43 AM
  2. stopping the thread
    By kailash in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-06-2011, 02:21 PM
  3. Stopping a thread
    By Arne in forum Threads and Synchronization
    Replies: 9
    Last Post: 10-21-2010, 11:26 AM
  4. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-05-2010, 12:15 AM
  5. Replies: 0
    Last Post: 04-04-2008, 03:46 PM

Tags for this Thread

Posting Permissions

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