7

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.

1
  • so use a Window that has the functionality , and customize to your alert Commented Aug 6, 2016 at 0:46

4 Answers 4

24
 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
Sign up to request clarification or add additional context in comments.

Works just fine even without stage.toFront();.
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.
Its 2020 and I found this very helpful for setting the stage to top of an alert dialog.
In my case, stage.toFront(); was necessary.
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));

Comments

10

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.

Comments

-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).

make sure you update your jdk and java tools and import for alert
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.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.