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 15 of 15

Thread: Swing TIMER

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Swing TIMER

    hey guy,

    I am trying to create a timer and i hope someone could help me to understand the following what do these lines of code mean?
     Timer timer = new Timer(1000, new TimerActionListener());// does this mean the speed?
     
     int count = 180;
    I would like the timer to start to be set to 1 minute and the timer to go backwards so 60,59,58,57 etc..


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Swing TIMER

    The Timer will call your listener code at the frequency/interval that the code sets.
    You can have that code do whatever you need done when it is called.

    If you want to see when the listener is called, add a println() statement that prints out a message when it is called.

    The place to start is the API doc. What does it say for each of the args to the Timer class's constructor?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing TIMER

    the problem is that the timer is all set up but the timer is going 60,61,62,63 but i need to go 60,59,58,57etc...
    Last edited by newtolearningjava; April 26th, 2014 at 06:10 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Swing TIMER

    the timer is going 60,61,62,63
    What line of code is changing the value of the "timer"?

    ++ means to add 1
    -- means to subtract 1
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing TIMER

    oh silly me.. one more on this. need to add it
    Last edited by newtolearningjava; April 26th, 2014 at 06:11 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Swing TIMER

    What happened when you used that code?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing TIMER

    i am not entirely sure on how to add this in my code.
    Last edited by newtolearningjava; April 26th, 2014 at 06:11 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Swing TIMER

    What happened when you tried as in post#5?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing TIMER

    when the code is run as stated in post#5 it is subtracting the digits and stops at 0.

    I would like an message to appear when it hits 0 to say you ran out of time you loose or somthing.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Swing TIMER

    The code in post#5 shows a call to the JOptionPane method inside an if.

          if (count ==0){
     
                        JOptionPane.showMessageDialog(null, "You loose", JOptionPane.DEFAULT_OPTION);
    That is what I am talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing TIMER

    my bad i looked at the wrong post, originally i have added here in this code but i get this error:
    Last edited by newtolearningjava; April 26th, 2014 at 06:11 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Swing TIMER

    Sorry, that's a message from the IDE, but it hides what the compiler error is. You need to try another way to get the compiler's error message.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing TIMER

    i have pressed ok?
    Last edited by newtolearningjava; April 26th, 2014 at 06:12 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Swing TIMER

    how do i get it to go away when i have pressed ok?
    Does it go away and a new one comes immediately?
    To keep the Timer from continuing to call the listener, stop or cancel it.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing TIMER

    thanks that did the trick

Similar Threads

  1. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 07:32 PM
  2. Swing Timer and Java Noob
    By hypnotoad in forum AWT / Java Swing
    Replies: 7
    Last Post: October 5th, 2012, 06:34 PM
  3. Timer implementation in swing
    By portem1 in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 19th, 2011, 08:14 AM
  4. Anyone familiar with the swing timer?
    By BigJoe in forum AWT / Java Swing
    Replies: 5
    Last Post: December 28th, 2010, 12:15 PM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM