Reputation: 93
I'm trying to display an alert on top of another alert in a full-screen JavaFX application. Without full-screen it works fine, the second alert is displayed on top of the first alert. With full-screen however the second alert is displayed under the first one despite having focus as shown in the first screenshot. Is it possible to have the second alert displayed on top?
Also as another issue regarding alerts in full-screen, when going to the desktop or alt+tab in Windows the alert is displayed despite setting the parent stage as owner as shown in the second screenshot (only the first alert for some reason). Is it possible to have the alerts tied to the full-screen parent stage so they aren't displayed when the parent stage isn't?
Example application:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void run(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Button button1 = new Button("CREATE 2 ALERTS");
button1.setOnAction(event -> {
createAlert("First alert", stage, 300, 300);
createAlert("Second alert", stage, 500, 100);
});
StackPane root = new StackPane(button1);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
}
private void createAlert(String text, Stage parentStage, int width, int height) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("");
alert.setContentText(text);
alert.initOwner(parentStage);
alert.getDialogPane().setPrefWidth(width);
alert.getDialogPane().setPrefHeight(height);
alert.show();
}
}
The problem has been replicated with two Windows 10 v10.0 machines, with java v1.8.0_161 javafx v8.0.161 and java v16.0.1 javafx v16
Upvotes: 2
Views: 393
Reputation: 93
After trying out the alwaysOnTop properties on the Alert Stages it seems there is some sort of bug that forces the first alert to be always on top even if the property is correctly false. This causes both the issues in the question.
Workaround that fixes both issues:
stageAlert1.setAlwaysOnTop(true);
stageAlert1.setAlwaysOnTop(false);
stageAlert2.toFront();
Example app fixed:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void run(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Button button1 = new Button("CREATE 2 ALERTS");
button1.setOnAction(event -> {
Alert alert1 = createAlert("First alert", stage, 300, 300);
Alert alert2 = createAlert("Second alert", stage, 500, 100);
Stage stageAlert1 = (Stage)alert1.getDialogPane().getScene().getWindow();
Stage stageAlert2 = (Stage)alert2.getDialogPane().getScene().getWindow();
stageAlert1.setAlwaysOnTop(true);
stageAlert1.setAlwaysOnTop(false);
stageAlert2.toFront();
});
StackPane root = new StackPane(button1);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
}
private Alert createAlert(String text, Stage parentStage, int width, int height) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("");
alert.setContentText(text);
alert.initOwner(parentStage);
alert.getDialogPane().setPrefWidth(width);
alert.getDialogPane().setPrefHeight(height);
alert.show();
return alert;
}
}
Upvotes: 2