I'm having a JavaFX-GUI that enables the user to scan. This process is taking a while. So the user should neither keep clicking around, nor should anyone think the program hung itself.
My solution was a modal window that is just in front of the whole GUI and and disappears itself when everything is finished.
The interface consists of a FXML-Controller, the FXML-File and the MainApp.
So far I'm calling it in the Controller:
Stage stage = Messages.beforeScan(event);
s.scan(filename);
stage.close();
The beforeScan
-Method looks like that (Taken from How to create a modal window in JavaFX 2.1 ):
public static Stage beforeScan(Event event) {
Stage stage = new Stage();
try {
Parent root = FXMLLoader.load(
FXMLController.class.getResource("/fxml/Scene.fxml"));
stage.setScene(new Scene(root));
stage.setTitle("My modal window");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(((Node)event.getSource()).getScene().getWindow());
stage.show();
} catch (IOException ex) {
Logger.getLogger(Meldungen.class.getName()).log(Level.SEVERE, null, ex);
}
return stage;
}
Yet this is not successful. If I comment out stage.close();
I can still use my main GUI.
So a different approach:
Instead of importing the Scene from the controller I tired to obtain it from the MainApp-Class:
public static Stage beforeScan(Event event) {
Stage stage = new Stage();
stage.setScene(MainApp.getStage.getScene());
stage.setTitle("My modal window");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(((Node) event.getSource()).getScene().getWindow());
stage.show();
return stage;
}
But this just crashes everything with a StackOverflow
.
The Popup (Taken from the example here ) is not helpful either. It is even only opened after the scan.
Using an Alert seemed like a good idea, but doing it that way is not only locking the screen but the program itself:
public Alert beforeScan(Event event) {
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.NONE, "Scan in progress");
alert.showAndWait();
return alert;
}
How should a screen-locking modal window/popup be done properly?