Results 1 to 4 of 4
Thread: Timer
- 01-12-2013, 10:09 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
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 }
Last edited by memcuch; 01-12-2013 at 10:31 PM.
-
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?
- 01-12-2013, 10:32 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Re: Timer
edited; Is it a little bit better now?;p
-
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
-
Timer
By biomechanics in forum AndroidReplies: 0Last Post: 06-30-2011, 01:48 AM -
Timer And Timer Task
By Ashish_jain in forum New To JavaReplies: 3Last Post: 06-22-2011, 10:41 AM -
timer
By Ashish_jain in forum New To JavaReplies: 10Last Post: 06-21-2011, 07:06 AM -
Stopping a Timer from Inside the timer
By krishnan in forum Java AppletsReplies: 2Last Post: 10-05-2010, 12:15 AM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 03:46 PM
Bookmarks