How to safely modify JavaFX GUI nodes from my own thread?

I am trying to change the JavaFX GUI node in a stream, but I see the following error:

Exception in thread "Thread-8" java.lang.IllegalStateException: Not included FX application thread; currentThread = Thread-8

Example code that generates an error:

public class Controller { public Label label = new Label(); public void load() { MyThread myThread = new MyThread(); myThread.start(); } public class MyThread extends Thread { public void run() { ...... label.setText(""); // IllegalStateException: Not on FX application thread } } } 
+6
source share
1 answer

All manipulations with JavaFX nodes in the graphics of the active scene must be performed in the JavaFX application stream, otherwise your program may not work correctly.

JavaFX throws an IllegalStateException: Not on FX application thread exception when trying to change the attributes of scene graph nodes from a JavaFX application thread. Even if you do not get an IllegalStateException, you should not change the nodes of the scene graph from the JavaFX application stream, because if your code can fail unexpectedly.

Replace the code that manipulates the scene graph nodes in Platform.runLater to allow the JavaFX system to run code in the JavaFX application thread.

For example, you can fix your program with the following code:

 Platform.runLater(new Runnable() { @Override public void run() { label.setText(""); } } 
+9
source

Source: https://habr.com/ru/post/957968/


All Articles