13

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()
user2513924
  • 2,070
  • 3
  • 15
  • 23
  • There seems to be no problem with the piece of code that you have provided. Can you check if the `thread.start();` is being called and thread is getting started. – ItachiUchiha Mar 27 '15 at 14:33
  • 1
    With the code you've posted, the `Thread` is redundant. You could simply call `Platform.runLater(...)` from whatever thread you're in. What's the context here? Have the FX toolkit and application thread been started? – James_D Mar 27 '15 at 14:46
  • Yeah sorry I've simplified it massively because there's a lot of other things in different classes in the thread. I absolutely know for a fact that the thread is started because that where my GLFW window comes from and I can see that. Basically I have my GLFW window/game and I want a Stage to also come up after a condition (so the GLFW window must come first). The FX application thread has started because I launch the GLFW window using an FX application. – user2513924 Mar 27 '15 at 16:23

1 Answers1

49

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!

asmmahmud
  • 4,844
  • 2
  • 40
  • 47
Draque Thompson
  • 908
  • 9
  • 16