Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX How to bring Dialog/Alert to the front of the screen

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.

like image 409
Xiao Lin Li Avatar asked Aug 05 '16 23:08

Xiao Lin Li


1 Answers

 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
like image 54
claimoar Avatar answered Nov 15 '22 03:11

claimoar