0

I use a JavaFX task to go through some initialisation process. The progress of that process shall be displayed on my GUI. Therefore I usesd a Task instead of "Platform.runLater". "Platform.runLater" does not update at runtime. At several points at the init process I need some interaction with the user. So I call a JavaFX "Alert" dialog. But the call to "new Alert(...);" do never return! If I use "Platform.runLater" the dialog appears but theres is no GUI update while the init process.

I'm trapped...

private static void mainInit() {
 // do some init stuff here...(and show it to the user)

 Alert alert = new Alert(AlertType.INFORMATION);
 // program never reaches this line...
 alert.setTitle("Hallo");
}


public void start(Stage primaryStage) {
  Task<Void> initTask = new Task<Void>() {
    @Override
    protected Void call() throws Exception {
      mainInit();
      return null;
    }
  };
  new Thread(initTask).start();
}
Uwe Andersen
  • 327
  • 2
  • 4
  • 15
  • 1
    Why do you need a Task to show the Alert?? You can just do it in your start methode – Developer66 Sep 28 '17 at 14:58
  • 3
    The call to `new Alert(...)` throws an exception, by the way, which you can see if you do `initTask.setOnFailed(e -> initTask.getException().printStackTrace());`. You **must** create and show the alert (but not do the rest of the processing) on the FX Application Thread. – James_D Sep 28 '17 at 15:18
  • @Developer66: If I do so, the GUI is not updating the init progress. – Uwe Andersen Sep 29 '17 at 08:29
  • @James_D: Yes, it is the same problem as in https://stackoverflow.com/questions/14941084/javafx2-can-i-pause-a-background-task-service ! I was able to fix it. Thank you so much! – Uwe Andersen Sep 29 '17 at 08:30

0 Answers0