I am trying to show two stages simultaneously in JavaFX, where the first stage is supposed to be showing a progessbar and which should close as soon as the second stage is ready to show. I tried running both via Platform.runLater and via Tasks but the problem is that the stages are both frozen until both are finished loading and the progessbar starts being animated not until the second stage is finished loading.
Here some extract from the code:
public void start(Stage primaryStage) throws Exception {
new Thread(progressTask()).start();
new Thread(loginTask()).start();
}
public Task<Void> loginTask() {
Task<Void> t = new Task<Void>() {
@Override
protected Void call() throws Exception {
Platform.runLater(new Runnable() {
@Override
public void run() {
Stage s = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml"));
Scene sc = null;
try {
sc = new Scene((Parent) loader.load());
} catch (IOException e) {
e.printStackTrace();
}
s.setScene(sc);
s.show();
}
});
return null;
};
};
return t;
}
public Task<Void> progressTask() {
Task<Void> t = new Task<Void>() {
@Override
protected Void call() throws Exception {
Platform.runLater(new Runnable() {
@Override
public void run() {
ProgressBar bar = new ProgressBar(0);
bar.setPrefSize(200, 24);
Timeline task = new Timeline(new KeyFrame(Duration.ZERO, new KeyValue(bar.progressProperty(), 0)), new KeyFrame(
Duration.seconds(5), new KeyValue(bar.progressProperty(), 1)));
VBox layout = new VBox(10);
layout.getChildren().setAll(bar);
Stage stage = new Stage(StageStyle.DECORATED);
stage.setScene(new Scene(layout));
stage.initModality(Modality.WINDOW_MODAL);
stage.show();
new Thread() {
public void run() {
task.playFromStart();
}
}.start();
task.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
stage.close();
}
});
}
});
return null;
}
};
Thanks for your help.