6

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:

  1. create a new stage and stage.setAlwaysOnTop(true), then alert.initOwner(stage).
  2. 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.

Community
  • 1
  • 1
Xiao Lin Li
  • 127
  • 1
  • 1
  • 9

4 Answers4

23
 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
claimoar
  • 261
  • 2
  • 3
  • 2
    Works just fine even without `stage.toFront();`. – Roshana Pitigala Apr 03 '17 at 08:49
  • At 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. – MINDoSOFT Aug 10 '17 at 14:30
  • Its 2020 and I found this very helpful for setting the stage to top of an alert dialog. – VijayKumar Feb 04 '20 at 08:55
  • In my case, `stage.toFront();` was necessary. – trilogy Jun 24 '22 at 13:51
10

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));
fabian
  • 80,457
  • 12
  • 86
  • 114
9

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.

Robert Mugattarov
  • 1,278
  • 2
  • 12
  • 26
-1

try this

 Alert a = new Alert(AlertType.ERROR);
        a.setTitle("Title of alert");
        a.initStyle(StageStyle.UNDECORATED);
        a.setContentText("details of message");
        a.showAndWait();

you can use this to force alert message to appear to the user if some thing wrong happen,for example when user enter the data as string and you accept data as integer. i hope that helps you.

note: i think Alert is available for javafx (jdk 1.8).

ali shreef
  • 69
  • 10
  • make sure you update your jdk and java tools and import for alert – ali shreef Aug 06 '16 at 00:23
  • This won't work even with an updated jdk. `javafx.scene.control.Alert` does not become `alwaysOnTop` automatically when the parent stage is OnTop unless you figure out a way to do so. – Roshana Pitigala Apr 03 '17 at 08:55