Why am I getting java.lang.IllegalStateException "Not on FX application thread" on JavaFX?
The user interface cannot be directly updated from a non-application thread. Instead, use Platform.runLater()
, with the logic inside the Runnable object. For example:
Platform.runLater(new Runnable() { @Override public void run() { // Update UI here. }});
As a lambda expression:
// Avoid throwing IllegalStateException by running from a non-JavaFX thread.Platform.runLater( () -> { // Update UI here. });
I had a similar issue which I fixed without using the Platform.runLater()
:
"java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-6"
Button play = new Button("Play");EventHandler<ActionEvent> playHandler = e1 -> { Runnable runnable = () -> { play.setText("Pause"); EventHandler<ActionEvent> timelineHandler e2 -> play(); timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler)); timeline.setCycleCount(Timeline.INDEFINITE); timeline.play(); }; Thread t = new Thread(runnable); t.start();};play.setOnAction(playHandler);
SOLVED! Move everything unrelated outside of the runnable
lambda:
Button play = new Button("Play");EventHandler<ActionEvent> playHandler = e1 -> { play.setText("Pause"); Runnable runnable = () -> { EventHandler<ActionEvent> timelineHandler e2 -> play(); timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler)); timeline.setCycleCount(Timeline.INDEFINITE); timeline.play(); }; Thread t = new Thread(runnable); t.start();};play.setOnAction(playHandler);