JavaFX How to bring a dialog / warning to the front of the screen

I want to force Alert to be on top of other applications. The alerts seem to lack the setAlwaysOnTop function.

I saw this post: JavaFX 2.2 Stage is always on top .

I 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's say I have a safari, and it is focused. I want to bring Alert to the top of the screen in front of the safari when I call its showAndWait () function.

+4
source share
5

"" DialogPane Alert Stage. alwaysOnTop :

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));
+6
 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
+13

fabians Claimoars :

((Stage) dialog.getDialogPane().getScene().getWindow()).setAlwaysOnTop(true);

Eclipse/JavaFX.

+2

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

, , - , , . , .

: , Alert javafx (jdk 1.8).

-1

JavaFX, , JavaFx , . , Java.

Try:

JOptionPane optionPane = new JOptionPane();
JDialog dialog = optionPane.createDialog("Title");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);            
-1

Source: https://habr.com/ru/post/1650371/


All Articles