Countdown Pause & Resume with Timer

872 views Asked by At

Here I have a question that make me big trouble.. I coded a countdown for a competition using java. Here is my interface

enter image description here

I used countdown() method to do my task like this..

public void countdown(){    
    TimerTask doit = new TimerTask() {
        @Override
        public void run() {
            c_s-=1;
            if (c_s == -1)
    {
      c_m -= 1;
      c_s = 59;
    }
    if (c_m == -1)
    {
      c_h -= 1;
      c_m = 59;
    }
    hour.setText(c_h+"");
    min.setText(c_m+"");
    sec.setText(c_s+"");

            if((c_h==-1)&& (c_m==59)&& (c_s==59)){

                time_countdown.cancel();
                JOptionPane.showMessageDialog(rootPane, "Invaild Time");
                System.exit(0);
            }
            if((c_h==0)&& (c_m==0)&& (c_s==0)){

                time_countdown.cancel();
                JOptionPane.showMessageDialog(rootPane, "Times Up");

            }


        }
    };
    time_countdown.scheduleAtFixedRate(doit, 0L, 1000L);
}

My case is how I pause and resume time..? I have no idea to do that..!!..If anyone can give me a solution by using this method, it will be easy for me.. (Do not care about Round 01 line. It is a stopwatch)

1

There are 1 answers

0
syck On

Maybe the easiest way would be if the pause button toggles a bool variable which tells the run method to exit right after being called (before any change happens) or go through it.