Home > Blockchain >  Can't update JLabels of minutes and hours for my chronometer in java.swing
Can't update JLabels of minutes and hours for my chronometer in java.swing

Time:02-13

I'm trying to make a stopwatch with java.swing and a stopwatch I have already tried the swing.Timer and TimerTask. Could you help me?

This is the stopwatch.java class

    public class Cronometro {
    private int secondi;
    private int minuti;
    private int ore;
    private boolean state=true;
    
    public Cronometro(){
        
    }
    
    public void crono(JLabel s, JLabel m, JLabel h, ActionEvent evt){
        state=true;
        
        Thread t = new Thread(){
            public void run(){
                for(;;){
                    if(state==true){
                       try{
                           sleep(10);
                           secondi  ;
                           if(secondi>60){
                               secondi=0;
                               minuti  ;
                               
                           }
                           if(minuti>60){
                               secondi=0;
                               minuti=0;
                               ore  ;
                           }
                           s.setText(" :" secondi);
                           m.setText(" : " minuti);
                           h.setText("" ore);
                       } catch(Exception e){
                           
                       }
                    }
                    else{
                        break;
                    }
                }
            }
        };
        
        t.start();
    }
    
    public void setState(boolean state){
        this.state=state;
    }
    
}

The gui/main.java was generated almost all by netbeans

CodePudding user response:

This is how you can use a Swing Timer to run update every second:

class Cronometro {
    private int secondi;
    private int minuti;
    private int ore;
    private boolean state=true;
    private Timer timer;

    private final JLabel s, m,  h;

    public Cronometro(JLabel s, JLabel m, JLabel h) {
        this.s = s;
        this.m = m;
        this.h = h;
    }

    public void crono(JLabel s, JLabel m, JLabel h){
        state=true;
        if(timer != null) {
            timer.stop();
        }

        timer = new Timer(1000, e->update());
        timer.start();
    }

    public void update(){

        if(! state) return;
        secondi  ;
        if(secondi>60){
            secondi=0;
            minuti  ;
        }

        if(minuti>60){
            secondi=0;
            minuti=0;
            ore  ;
        }
        s.setText(" :" secondi);
        m.setText(" : " minuti);
        h.setText("" ore);
    }

    public void setState(boolean state){
        this.state=state;
    }
}

Side note: to improve the application structure introduce a model class that encapsulates the needed information (seconds, minutes, hours). Share this model between Cronometro and the GUI.

  • Related