Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Timer wont stop...

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Timer wont stop...

    Hi,
    I made a timer, and it wont stop... I wanted it to only initiate the action once, so i setReapets(false) but it still just keeps on going. Even when i but timer.stop() it keeps on going... How do i fix this?

    import javax.swing.*;
    import java.awt.*;
    import javax.swing.Timer;
    import java.awt.event.ActionEvent;
     
    public class TimerTest
    {
        public static void main(String[] args) {
            JFrame frame = new JFrame ("Timer Test");
            frame.setSize (400,400);
            JPanel panel = new JPanel();
            // make new action
            Action action = new AbstractAction() {
                public void actionPerformed (ActionEvent e){
                    System.out.println ("hello");
        }
    };
    Timer timer;
    timer = new Timer(5000, action);
    timer.setRepeats(false);
    System.out.println ("The timer has started");
    timer.start();
    timer.stop();
    }
    }


  2. #2

    Default Re: Timer wont stop...

    I would use java.util.Timer.

    It has scheduling methods for single use and repeated use.

    Single use:
    Timer timer = new Timer();
    		timer.schedule(new TimerTask()
    		{
     
    			@Override
    			public void run()
    			{
    				//put code here
    			}
    		}, longDelay);

    Repeat use:
    Timer timer = new Timer();
    		timer.scheduleAtFixedRate(new TimerTask()
    		{
     
    			@Override
    			public void run()
    			{
    				//put code here
    			}
    		}, longDelay, longPeriodBeforeRepeat);
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Timer wont stop...

    The Swing Timer works fine. I ran that code, and it runs exactly how I'd expect- it prints "hello" once. Are you sure you're running this code? Try deleting any .class files, recompiling, and trying again.
    Last edited by KevinWorkman; October 6th, 2011 at 10:30 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Timer wont stop...

    Hey, I got it to work. I dont know what was wrong with it before, but when i switched computers it started working fine. However, i have moved to my home computer now instead of my school one and i use eclipse. Eclipse is giving me an error on AbstractAction saying that "The serializable class does not declare a static final serialVersionUID feild of type long". The code compiles and runs, but the action isnt working. So the timer starts (i think) but the action doesnt work...
    Any advice?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Timer wont stop...

    That's not an error, it's just a warning, and it's pretty self-explanatory. It should not affect runtime behavior at all.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Timer wont stop...

    Hey, i downloaded BlueJ and my code worked in that, so there must be something wrong with my eclipse... Ill let you know if i figure it out...

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Timer wont stop...

    Quote Originally Posted by pottsiex5 View Post
    Hey, i downloaded BlueJ and my code worked in that, so there must be something wrong with my eclipse... Ill let you know if i figure it out...
    I highly doubt it. You were probably just using an old .class file. Simply cleaning the project in eclipse would have probably done the trick, but you never followed my advice to delete the .class files and recompile.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Timer wont stop...

    I did take your advice and recompile it. Though i did find out that if i reduce the speed to anywhere below 1062 milliseconds it works... I also noticed in the title of the console it says terminated<TimerTest.java> before the 5 seconds are up, so it is ending the file before the timer has a chance to output the "hello"...

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Timer wont stop...

    Quote Originally Posted by pottsiex5 View Post
    I did take your advice and recompile it. Though i did find out that if i reduce the speed to anywhere below 1062 milliseconds it works... I also noticed in the title of the console it says terminated<TimerTest.java> before the 5 seconds are up, so it is ending the file before the timer has a chance to output the "hello"...
    Yup, that would make sense. Try popping up a JFrame to keep the program going.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM
  2. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM
  3. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM
  4. Compliing fine but wont run--please help!
    By Nova in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2010, 07:51 PM
  5. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM