JavaFX How to bring Dialog/Alert to the front of the screen
13,464
Solution 1
You could "steal" the DialogPane
from a Alert
and show it in a utility Stage
. For this window you can set the alwaysOnTop
property the usual way:
Alert alert = new Alert(Alert.AlertType.WARNING, "I Warn You!", ButtonType.OK, ButtonType.CANCEL);
DialogPane root = alert.getDialogPane();
Stage dialogStage = new Stage(StageStyle.UTILITY);
for (ButtonType buttonType : root.getButtonTypes()) {
ButtonBase button = (ButtonBase) root.lookupButton(buttonType);
button.setOnAction(evt -> {
root.setUserData(buttonType);
dialogStage.close();
});
}
// replace old scene root with placeholder to allow using root in other Scene
root.getScene().setRoot(new Group());
root.setPadding(new Insets(10, 0, 10, 0));
Scene scene = new Scene(root);
dialogStage.setScene(scene);
dialogStage.initModality(Modality.APPLICATION_MODAL);
dialogStage.setAlwaysOnTop(true);
dialogStage.setResizable(false);
dialogStage.showAndWait();
Optional<ButtonType> result = Optional.ofNullable((ButtonType) root.getUserData());
System.out.println("result: "+result.orElse(null));
Solution 2
Alert alert = new Alert(Alert.AlertType.WARNING, "I Warn You!", ButtonType.OK, ButtonType.CANCEL);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.setAlwaysOnTop(true);
stage.toFront(); // not sure if necessary
Solution 3
I have tried fabians and claimoars solutions and simplified them to:
((Stage) dialog.getDialogPane().getScene().getWindow()).setAlwaysOnTop(true);
This works in my Eclipse / JavaFX app.
Author by
Xiao Lin Li
Updated on June 20, 2022Comments
-
Xiao Lin Li almost 2 years
I want to force an Alert to be on top of other applications. Alerts seems to be lacking a setAlwaysOnTop function.
I have seen this post: JavaFX 2.2 Stage always on top.
I have tried:
- create a new stage and stage.setAlwaysOnTop(true), then alert.initOwner(stage).
- create a new stage and stage.initModality(Modality.APPLICATION_MODAL), then alert.initOwner(stage).
Does anyone know how to achieve this?
Edit: I am using Java 8.
Let say I have safari opened, and it is being focused. I want to bring the Alert to the top of the screen, in front of safari, when I call its showAndWait() function.
-
ali shreef over 7 yearsmake sure you update your jdk and java tools and import for alert
-
Roshana Pitigala about 7 yearsWorks just fine even without
stage.toFront();
. -
Roshana Pitigala about 7 yearsThis won't work even with an updated jdk.
javafx.scene.control.Alert
does not becomealwaysOnTop
automatically when the parent stage is OnTop unless you figure out a way to do so. -
Kefirchiks almost 7 yearsThanks. Works great!
-
MINDoSOFT over 6 yearsAt first I tried it without stage.toFront(); and it didn't work. Then I tried it with stage.toFront() and it worked! Thank you very much for this solution! Afterwards I saw that I forgot to call alert.initOwner(App.getPrimaryStage()); and this was causing the bug in my case.
-
VijayKumar about 4 yearsIts 2020 and I found this very helpful for setting the stage to top of an alert dialog.
-
trilogy almost 2 yearsIn my case,
stage.toFront();
was necessary.