How do I set an Alert to be always on top?
Asked Answered
C

1

6

Lets say I have a simple alert.

Alert a = new Alert(AlertType.NONE);

CustomLabel lbl = new CustomLabel("Testing");
CustomButton ok = new CustomButton("OK");
FlowPane fp = new FlowPane(); 
fp.getChildren().addAll(lbl,ok);

alert.getDialogPane().setContent(fp);
alert.initStyle(StageStyle.TRANSPARENT);

Then I would like this to make this alert always on top of whatever screen I am showing, how do I do it?

Setting the stage of my main page to.

mainStage.setAlwaysOnTop(false); //does not work

Both my main and alert are to be on the same thread.

Meaning, on running my main, it will check for something then pop up the alert if something is not right.

Cacogenics answered 4/1, 2017 at 10:4 Comment(0)
M
8

Set your main stage to be always on top:

mainStage.setAlwaysOnTop(true);

Then set your main stage to be owner of Alert dialog:

a.initOwner(mainStage);

If main stage is owner of alert dialog, then alert will always be on top of main stage.

Matabele answered 4/1, 2017 at 10:28 Comment(1)
To get the main stage, you could use a injected element (@FXML) and then (Stage) <injected element>.getScene().getWindow();Behest

© 2022 - 2024 — McMap. All rights reserved.