-->

Javafx Platform.runLater never running

2020-08-17 07:30发布

问题:

I basically want to be able to launch a new Javafx window (stage) after (and inside) my LWJGL/GLFW thread starts. I am basically doing:

Thread thread = new Thread(()->Platform.runLater(()->{
    Stage stage = new Stage();
    //Stage setup
    stage.show();
}));
thread.start();

thread being my game thread. But it never runs and I've tried a System.out.println() inside Platform.runLater() just to check it never runs.

Why does it never run and what can I do to fix it? Thanks.

EDIT: Just to clarify that the thread has definitely started and whatnot, if I do:

Thread thread = new Thread(()->{
    System.out.println("Before Platform.runLater()");
    Platform.runLater(()->System.out.println("Inside Platform.runLater()"));
    System.out.println("After Platform.runLater()");
});

It outputs:

Before Platform.runLater()
After Platform.runLater()

回答1:

Ok, I have found the solution to this!

If you run into any situation where all of your scenes end, the thread managing all of this will just peter out. To prevent this from happening, add this line before Platform.runLater:

Platform.setImplicitExit(false);
Platform.runLater(()->System.out.println("Inside Platform.runLater()"));

So long as this runs before the last scene ends, it will keep the thread alive, and you will not run into the problem of runLater() failing!