How to set a timer on JLabel to update itself every second

I created a game, and in my swing GUI I want to set a timer. The way I am doing this at the moment has a field with the current time, obtained with the help System.currentTimeMillis(), which gets its value when the game starts. In the method of my game, I put a field System.currentTimeMillis(); and he tells you the current time since the start of the game.

However, how to do this to update every second, say, so it JLabelwill have: timePassed: 0s, timePassed: 1s, etc. Keep in mind that I do not use threads in my game at any time.

EDIT: Thank you all for your kind suggestions. I used a combination of your answers, please give me some feedback.

I have jlabel as a field called time. (otherwise I can't handle it).

time = new JLabel("Time Passed:  " + timePassed() + " sec");
    panel_4.add(time);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            time.setText("Time Passed: " + timePassed() + " sec");
        }
    };
    Timer timer = new Timer(1000, actionListener);
    timer.start();
+3
source share
4 answers

See the swing timer class . This makes it easy to set up repetitive tasks.

+5
source

Here is how I would install my JLabel to update with time and date.

Timer SimpleTimer = new Timer(1000, new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        jLabel1.setText(SimpleDay.format(new Date()));
        jLabel2.setText(SimpleDate.format(new Date()));
        jLabel3.setText(SimpleTime.format(new Date()));
    }
});
SimpleTimer.start();

Then it is added to your main class, and jlabel1 / 2/3 is updated with a timer.

+6
source
new Thread(new Runnable
{
    public void run()
    {
        long start = System.currentTimeMillis();
        while (true)
        {
            long time = System.currentTimeMillis() - start;
            int seconds = time / 1000;
            SwingUtilities.invokeLater(new Runnable() {
                 public void run()
                 {
                       label.setText("Time Passed: " + seconds);
                 }
            });
            try { Thread.sleep(100); } catch(Exception e) {}
        }
    }
}).start();
+3
source

wirite is in the constructor

ActionListener taskPerformer = new ActionListener () {

             @Override

            public void actionPerformed(ActionEvent evt) {
               jMenu11.setText(CurrentTime());
            }
        };

        Timer t = new Timer(1000, taskPerformer);
        t.start();

And this is Write Out Constructor

public String CurrentTime () {

       Calendar cal = new GregorianCalendar();
       int second = cal.get(Calendar.SECOND);
       int min = cal.get(Calendar.MINUTE);
       int hour = cal.get(Calendar.HOUR);
       String s=(checkTime(hour)+":"+checkTime(min)+":"+checkTime(second));
       jMenu11.setText(s);
       return s;

   }

   public String checkTime(int t){
String time1;
if (t < 10){
    time1 = ("0"+t);
    }
else{
    time1 = (""+t);
    }
return time1;

}

0
source

All Articles