user2947605
user2947605

Reputation:

How to show a new Stage from my own thread in JavaFX?

I have to run some task in another thread and when some condition has been met in that threads run() method I must display a pop-up window with a a picture inside it, however trying to do that causes an Exception that tells me that I can't create any stages outside the main UI thread, now is there a way to go around this, can I somehow access the UI thread from my custom thread and make it show the stage? (Note: because of the nature of the application, I must use a custom thread for checking the condition, because there is like 8 different threads checking the same condition at the same time.)

The exception:

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

Upvotes: 1

Views: 1864

Answers (1)

Schidu Luca
Schidu Luca

Reputation: 3947

You should wrap your actions with Platform.runLater(...)

new Thread(() -> {
      Platform.runLater(() -> { 

          /* Your code goes here */

      });   
}).start();

Upvotes: 4

Related Questions