griFlo griFlo - 3 months ago 23
Java Question

Javafx Dialog (Alert) not visible if owner is minimized

Why is a Dialog (Alert) in javafx not correctly shown if the owner is minimized.

look at the following Code:

public class JavaFxSample2 extends Application {

@Override
public void start(Stage primaryStage) throws InterruptedException, ExecutionException {
primaryStage.setTitle("open alert in 2 seconds");
Pane pane = new Pane();
Button b = new Button("open dialog");
b.setOnMouseClicked(event -> {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
Thread.sleep(2000);
return null;
}
};
task.stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == State.SUCCEEDED) {
Alert alert = new Alert(AlertType.INFORMATION, "Hello");
alert.initOwner(primaryStage);
alert.showAndWait();
}
});
Thread thread = new Thread(task);
thread.start();
});

pane.getChildren().add(b);

Scene scene = new Scene(pane, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}


If you minimize the application within the 2 seconds, and then maximize it again (after the 2 seconds) you dont see the dialog but it is there somehow (the stage is locked till you press esc or enter)

if you dont minimize the stage the dialog is shown correctly.

is this a bug? am i doing it wrong?

Edit:
System is Windows 7, Java 1.8.0.66

Edit2:
Ok. it seems like its really a bug:
https://bugs.openjdk.java.net/browse/JDK-8151170

Answer

Found a (possible) solution. But is this really a good solution?

before showing the Alert I execute the following line:

((Stage) alert.getOwner()).setIconified(false);

If someone has a better idea i'll delete my answer.

Comments