I create a new stream and from it I need to display the value of t1 in Label

public void Seconds(){ new Thread(() -> { finish.set(true); while (finish.get()){ a1++; t1 = Integer.toString(a1); Label.setText(t1); try { Thread.sleep(999); } catch (InterruptedException ex) { ex.printStackTrace(); } } }).start(); } 

But instead of displaying values, I get an error that this is not possible. How, then, to display the required value on the screen? Via System.out.println (t1); everything is working.

    1 answer 1

      public void Seconds(){ new Thread(() -> { finish.set(true); while (finish.get()){ a1++; t1 = Integer.toString(a1); Platform.runLater(new Runnable() { @Override public void run() { Label.setText(t1); } }); try { Thread.sleep(999); } catch (InterruptedException ex) { ex.printStackTrace(); } } }).start(); } 

    Plutform.runLater executes code in a user interface thread. Any changes to the interface must be made through it.

    • one
      Considering the fact that it’s most likely Java8, instead of implementing Runnable, you can shove lambda and shorten the code Platform.runLater (() -> Label.setText (t1)) - ActivX 1:32 pm