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

5k views Asked by At

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.

1

There are 1 answers

1
MBec On BEST ANSWER

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.