0

I am a student, I am creating a clock for an application, I added a Label in scene builder assigned the clock to it and added the following java code:

 public void initialize() throws FileNotFoundException, InterruptedException {


   var now = LocalTime.now();
   DateTimeFormatter dft =DateTimeFormatter.ofPattern("HH:mm:ss");
   
labelclock.setText(LocalTime.now().format(dft));

I want to make the clock tick and tried in many ways but it didn't work.

I also tried creating the clock in another way and got the same result.

 public void setTime() {

       while (true) {

       time = timeFormat.format(calendar.getInstance().getTime());
       labelclock.setText(time);
         try {
          Thread.sleep(1000);
              } catch (InterruptedException e) {
          e.printStackTrace();
              }
             }
            }

AKL-R
  • 1
  • 2
  • 1
    This is no way to make a clock. Instead of `while(true) {`, use a Timer thread. [Read this](https://www.educba.com/javafx-timer/). Also read up on the **Clip** class. – DevilsHnd - 退職した Dec 23 '22 at 22:51
  • 1
    @DevilsHnd A timer here is not a good approach. It unnecessarily creates additional threads and the.l code becomes more convoluted to make sure UI updates occur on the correct thread. The animation API is the preferred approach here. – James_D Dec 24 '22 at 00:49
  • https://stackoverflow.com/questions/60226658/trying-to-stop-a-single-thread-out-of-multiple-running-at-the-same-time-in-java/60234199#60234199 – SedJ601 Dec 27 '22 at 05:34
  • https://stackoverflow.com/questions/50766244/creating-a-timer-that-shows-the-time-in-hhmmss-format-javafx/50774740#50774740 – SedJ601 Dec 27 '22 at 05:35
  • https://stackoverflow.com/questions/50626831/how-to-set-up-two-timelines-to-one-app/50627639#50627639 – SedJ601 Dec 27 '22 at 05:36

0 Answers0