How to Stop Javax.Swing.timer?

2019-08-06 13:31发布

I'm a java newbie and I'm creating a scheduler program using netbeans. My software will get Time and action from the user and enter those data in to separate arraylists. I've then used javax.swing.timer to start a countdown timer. When user finished entering data and hit the "run" button,The timer will get the running time from the first element in the array list and start counting down. when the countdown time reach 0 , timer will get the count down time from the next element in the array list, and so on.

My problem is, the timer runs perfectly but when there is no more elements in the arraylist, it still won't stop. Please help me with this. Here's the part of my code I'm having trouble with:

private void btnstartRunActionPerformed(java.awt.event.ActionEvent evt) {                                            
  countdown = ActionTime.get(0);
  Action.setText(ActionName.get(c));
  timer = new Timer(1000,new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent event){

      if(countdown<=0){
          try{
            c++;
            Action.setText(ActionName.get(c));
            //this nested if statement is to check whether the array has any more elements.
            //(This is the part that doesn't work.)
            if(c >= ActionTime.size()){
              timer.stop();
              JOptionPane.showMessageDialog(null, "Workout Completed!");
            }  
            countdown=ActionTime.get(c);
            timer.restart();

          } catch (Exception error2){
          }
       }
       Time.setText(Integer.toString(countdown));
       countdown--;
     }
  });
  timer.start();
}                                           

标签: java swing timer
2条回答
一夜七次
2楼-- · 2019-08-06 13:38

here is an example for you:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Main extends JFrame {
    Timer timer;

    int counter;

    Main(String title) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ActionListener a = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Counter = " + counter);

                if (++counter > 10) {
                    timer.stop();
                    System.exit(0);
                }
            }
        };

        timer = new Timer(300, a);
        timer.start();

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Main("Timer Demo1");
    }
}
查看更多
我命由我不由天
3楼-- · 2019-08-06 13:58

You should change:

            c++;
            Action.setText(ActionName.get(c)); //move this down
            if(c>=ActionTime.size()){
                timer.stop();
                JOptionPane.showMessageDialog(null, "Workout Completed!");
            }

to:

            c++;
            if(c>=ActionTime.size()){
                timer.stop();
                JOptionPane.showMessageDialog(null, "Workout Completed!");
                return; //add this too as the program should not continue with below
            }
            Action.setText(ActionName.get(c)); //to here

Because when you attempt to do ActionName.get(c) when the value of c exceeds the maximum possible value, the get method will throw an Exception. That leads us to the second mistake you're making:

           catch(Exception error2){

           }

Doing this makes the program ignore the Exception and continue as though nothing happened. An easy-to-make mistake that deceived you into thinking something else is wrong, but now you know! Change it to:

           catch(Exception error2){
                error2.printStackTrace();
                System.exit(-1); //or some error handling
           }

Or remove the try/catch block so that the Exception will end your program or is handled some other way.

查看更多
登录 后发表回答