Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+23 votes

Can someone help me with modifying Label inside the Runnable's run() method. I tried multiple times and always get this:

Exception in thread "pool-2-thread-1" java.lang.IllegalStateException: Not on FX application thread; currentThread = pool-2-thread-1

asked in CSC285_Fall2018 by (100 points)

1 Answer

+4 votes

Any code that modifies visual components in JavaFX needs to be run from the JavaFX application thread -- it can't be run directly from a separate thread.

However, the separate thread (e.g. the Runnable you've created) can ask the JavaFX thread to run some code the next chance it gets. This is done using:

Platform.runLater(() -> { /* set label text or do other gui stuff here */ }).

Note: We did this in the early lab that involved having a separate thread causing the video to auto-play. We also used this code in the handleTrackedFrame(...) method that gets called by the AutoTracker's separate thread.

answered by (1.4k points)
...