Results 1 to 4 of 4

Thread: Timer

  1. #1
    memcuch is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Timer

    Hi, I have timer declared in my code:

    Java Code:
    ActionListener listener;     //listener for timer
    listener = new ActionListener(){
    
    public void actionPerformed(ActionEvent event)
    {//counting down from 6 to 0
     if(tajmery==0){                                           //tajmery is a "switching" variable; =0, counting down, =1 counting up;
          time--;
          jLabel10.setText(Integer.toString(time));   //display value of variable "time"
             if(time==0){                                           //when time reaches 0, timer starts to count up;
                tajmery=1;
                jButton22.setEnabled(true);                   //unlocking 1 button
                jButton2.setIcon(jButton10.getIcon());    //loading image from "buffer" jButton10
                jButton3.setIcon(jButton10.getIcon());
                jButton4.setIcon(jButton10.getIcon());
                jButton5.setIcon(jButton10.getIcon());
                jButton6.setIcon(jButton10.getIcon());
                jButton7.setIcon(jButton10.getIcon());
                time = 0;                                            //restart variable time
                jButton1.setEnabled(false);                  //nothing important
               }\\if
    }//if
    
    //counting from 0 to infinity
     if(tajmery==1){ 
             if(timestate==1){                                   //timestate==1 -> time is icreasing, 0 -> stop
               czas++;
               jLabel10.setText(Integer.toString(czas));  
             }//if
     }//if
     }//void
    }; //listener                  
    Timer tajmer = new Timer(1000, listener);//1 000 ms = 1s delay     //new timer
    tajmer.start();                                                                               //start
    }
    And now I would like to turn this timer off using jButton, because when I am using dispose() and running frame again, I have 2 timers working at the same time. How I can do this?
    Last edited by memcuch; 01-12-2013 at 10:31 PM.

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

    Default Re: Timer

    Please edit your code and fix your formatting, in particular your indentation. Indentation is not just for good looks, but rather it helps you to see at a glance where blocks of code start and finish, and what *scope* variables are declared (and that is likely the cause of your current problem, but it's so hard to tell with that awful formatting). This isn't asking too much since it is you who are asking volunteers to donate time to help you. So why not first help us by making your code much easier to read?

  3. #3
    memcuch is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Timer

    edited; Is it a little bit better now?;p

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

    Default Re: Timer

    So is your Swing Timer variable declared in the class? If not, you may wish to do so. Then in the JButton's ActionListener simply call stop() on the Timer variable. You can and should also check to see if the Timer is non-null and is running before starting it anew.

Similar Threads

  1. Timer
    By biomechanics in forum Android
    Replies: 0
    Last Post: 06-30-2011, 01:48 AM
  2. Timer And Timer Task
    By Ashish_jain in forum New To Java
    Replies: 3
    Last Post: 06-22-2011, 10:41 AM
  3. timer
    By Ashish_jain in forum New To Java
    Replies: 10
    Last Post: 06-21-2011, 07:06 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

Posting Permissions

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