20

I have Label label in my FXML Application.

I want this label to change once a second. Currently I use this:

        Task task = new Task<Void>() {
        @Override
        public Void call() throws Exception {
            int i = 0;
            while (true) {
                lbl_tokenValid.setText(""+i);
                i++;
                Thread.sleep(1000);
            }
        }
    };
    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();

However nothing is happening.

I don't get any errors or exceptions. I don't need the value I change the label to in my main GUI thread so I don't see the point in the updateMessage or updateProgress methods.

What is wrong?

Killerpixler
  • 4,200
  • 11
  • 42
  • 82

2 Answers2

38

you need to make changes to the scene graph on the JavaFX UI thread. like this:

Task task = new Task<Void>() {
  @Override
  public Void call() throws Exception {
    int i = 0;
    while (true) {
      final int finalI = i;
      Platform.runLater(new Runnable() {
        @Override
        public void run() {
          label.setText("" + finalI);
        }
      });
      i++;
      Thread.sleep(1000);
    }
  }
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
zhujik
  • 6,514
  • 2
  • 36
  • 43
  • Studying your example helped me understand the JavaFX concurrency better. Thanks a ton. – Makarand Nov 29 '14 at 07:06
  • I included this code in initialize() method but i'm getting Null pointer exception at the line "label.setText("" + finalI);" – user1335906 Jan 22 '15 at 11:38
  • @user1335906 make sure that your label is set in the fxml file. – zhujik Jan 22 '15 at 13:26
  • where do you put this code snippet? ButtonAction? Main? Extra class? Which type of Task should i use? There are multiple "Task" declarations in JavaFX – Rubinum Jun 08 '15 at 13:21
  • When i do `System.out.println(Thread.currentThread().getId())` inside `call()` it prints the same id as in `main`. Is this normal? – OrangePot Jun 29 '16 at 11:20
  • I have the same problem. But it doesn't solve my problem. It prints all logs print fire event after all process is done. – Sachin Chandil Apr 10 '17 at 05:27
14

Cosmetic change to Sebastian's code.

 while (true)
 {
   final int finalI = i++;
   Platform.runLater ( () -> label.setText ("" + finalI));
   Thread.sleep (1000);
 }
dmolony
  • 1,125
  • 9
  • 23
  • 1
    this is far more important than a cosmetic change as it allows for easy variable passing. this is the more correct method. – calben Oct 21 '15 at 00:41