简体   繁体   中英

unable to stop javax swing timer

i have a problem trying to stop my Timer. I have two different classes, the first contains the button that starts and stop the button and the second is the chrono class. Here is the button:

btnStopstart = new JButton("START");
btnStopstart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {


    Chrono1 cn = new Chrono1(chrono);
    String texte = btnStopstart.getText();
    if(texte.equals("START")){
        btnStopstart.setText("STOP");
        try {
            cn.Editchrono(texte);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }else if(texte.equals("STOP")){
        btnStopstart.setText("START");
        cn.Editchrono(texte);
    }

}

});

And here is the chrono class:

public class Chrono1 {
private static int sec;
private JTextField chrono;
public Chrono1(JTextField chrono){
    this.chrono = chrono;
}
public void Editchrono(String txt){
    /* Le timer */
    int delais=1000;
    ActionListener tache_timer;
    tache_timer = new ActionListener(){
        public void actionPerformed(ActionEvent e){
            sec++;

            if(sec == 15 ){
                //Conditions
            }
            if(sec == 16){
                /*On realise une pause de 1 sec */
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                //mettre les conditions ici
                sec = 0;

            }
            //System.out.println(sec);

            chrono.setText("" + sec);
        }
    };
    final Timer timer1= new Timer(delais,tache_timer);
    if(txt.equals("START")){
        timer1.start();
    }else if(txt.equals("STOP")){
        timer1.stop();
        //sec = 0;
    }

}

}

Thank you for the help.

You're calling stop() on a Swing Timer all right, but not on the Timer instance that's running. Rather you're calling stop() on a completely new Timer instance, one that's not even running, and one that is completely unrelated to the Timer that is in fact running. You need to give the Chrono1 class a Timer field, say called timer , set this field to reference the running Timer when it starts, and call stop on this field (if not null) when stop is called. You also need to create one and only one Chrono1 object.

eg,

public class Chrono1 {
    private static int sec;
    private JTextField chrono;
    private Timer timer1; // ***** added ***

    public Chrono1(JTextField chrono){
        this.chrono = chrono;
    }

    public void Editchrono(String txt){
        int delais=1000;
        ActionListener tache_timer;
        tache_timer = new ActionListener(){
            public void actionPerformed(ActionEvent e){

                // .... etc.....

            }
        };

        if(txt.equals("START")) {

            // **** note changes? ****
            // final Timer timer1= new Timer(delais,tache_timer);  // ** no **
            timer1= new Timer(delais,tache_timer);  // ** yes! **
            timer1.start();
        }else if(txt.equals("STOP")){
            if (timer1 != null && timer1.isRunning()) {
                timer1.stop();
            }
        //sec = 0;
        }
    }
}

Also this guy shouldn't be re-created:

    Chrono1 cn = new Chrono1(chrono);

And so this should be a private instance field of either the whole class or of the inner ActionListener class and not re-created with each button push.

eg, make the changes below:

btnStopstart.addActionListener(new ActionListener(){
    private Chrono1 cn = new Chrono1(chrono); // **** add this 

    public void actionPerformed(ActionEvent e) {
        // Chrono1 cn = new Chrono1(chrono); // **** remove this 
        String texte = btnStopstart.getText();
        if(texte.equals("START")){
            btnStopstart.setText("STOP");
            try {
                cn.Editchrono(texte);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else if(texte.equals("STOP")) {
            btnStopstart.setText("START");
            cn.Editchrono(texte);
        }
    }
});    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM