D
D
Dmitry Borisenko2018-11-23 08:03:56
Java
Dmitry Borisenko, 2018-11-23 08:03:56

Hello! Why can't the run() method change the state of Swing components?

I am making a program - a chronometer (clock, timer, alarm clock, stopwatch, time calculations). To display the current time, I used the Timer class, and everything is displayed on the panel without any problems. But for the stopwatch, I decided to use a JPanel that implements Runnable. Everything works great in the console. But the run() method does not change the state of the labels on the panel. Tell me, please, what is the problem. I am not a professional programmer, so general phrases like "smoke manuals" or "use JavaFX" are not needed. If you know, just specify the path a little more specifically!)

@Override
  public void run() {
    long time_start = Calendar.getInstance().getTimeInMillis();
    long time_current = 0L;
    while (true) {
      time_current = Calendar.getInstance().getTimeInMillis() - time_start;
      hours = time_current/3600000;
      time_current = time_current % 3600000;
      minutes = time_current/60000;
      time_current  = time_current % 60000;		
      seconds = time_current/1000;
      time_current = time_current % 1000;
      santiSec = time_current/10;
                  
      System.out.println(time_current + ", santi = " + santiSec + ", seconds= " + seconds + ", minutes =" + minutes + ", hours = " + hours);
      lblMillisec_0.setIcon(Clock_Face.iconSmallDigits[1][(int) (santiSec/10)]);
      lblMillisec_1.setIcon(Clock_Face.iconSmallDigits[1][(int) (santiSec%10)]);
      try {
        Thread.sleep(10);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question