Skip to content
Advertisement

Can’t make changes to interface in a runnable with JavaFX

In my program, I am running a separate script and I am using concurrency to run the separate script, and therefore using a class implementing Runnable.

When the the separate thread is running, I want there to be a different graphic to when the thread is not running so the user can see if something is happening after pressing the button.

The function of the thread is working fine, it is just that after the thread has finished, the button graphic does not change when I try to change the graphic from inside the thread.

Code related to the issue:

In the class:

JavaScript

In the thread:

JavaScript

Advertisement

Answer

You cannot change the user interface outside the application thread. All user interface code checks isFxApplicationThread() and throw an exception if not.

You either need to call runLater to ensure your update is executed on the application thread, or implement a Task

To learn more about concurrency in JavaFX you can check out the Concurrency in JavaFX trail.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement