griFlo
griFlo

Reputation: 2164

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

Upvotes: 2

Views: 1552

Answers (4)

Wiitry
Wiitry

Reputation: 19

Here's a static method, embedding the previous tips of @Lurking Elk.

It automates the process, and then restores the parent to its current “iconified” state.

public static <T> Optional< T > Dialog_showAndWait( Dialog< T > _dialog )
{
    /**
     * a) PATCH:
     *      IF( dialog.Parent is iconified )
     *      THEN de-iconify the Parent
     */
    Boolean isParentIconized = null;
    Stage   parentStage = ( (Stage) _dialog.getOwner() );
    if( parentStage != null )
    {
        isParentIconized = parentStage.isIconified();
        if( isParentIconized )
            parentStage.setIconified( false );
    }

    /**
     * b) Process the original function
     */
    Optional< T > result = _dialog.showAndWait();

    /**
     * c) Restore the initial state of the dialog's Parent
     */
    if( parentStage != null )
    if( isParentIconized )
        parentStage.setIconified( isParentIconized );

    return result;
}

Upvotes: 1

Rafael Gimenes
Rafael Gimenes

Reputation: 186

DaoAlloyGeneral a = (DaoAlloyGeneral) tabelaAlloy.getSelectionModel().getSelectedItem();

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(DaoCfgIdiomas.getTraducao("general.Confirmacao"));
alert.setHeaderText(DaoCfgIdiomas.getTraducao("general.Atencao"));
alert.setContentText(DaoCfgIdiomas.getTraducao("MainTabAlloy.msgDeletar") + " " +a.getNome());
alert.setResizable(true);
alert.getDialogPane().setPrefSize(480, 320);

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){                 
    DaoAlloyGeneral.deletarAlloy(a.getIdAlloy());
    tabelaAlloy.getItems().clear();
    tabelaAlloy.setItems(DaoAlloyGeneral.buscaTodos());
}

enter image description here

I cant understood it worked before and now, stop. Oracle jdk1.8.0_111 linux machine

EDIT: I found the problem, JVM argument -Dcom.sun.javafx.isEmbedded=true Without this option it worked.

Upvotes: 0

griFlo
griFlo

Reputation: 2164

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.

Upvotes: 5

Rana Depto
Rana Depto

Reputation: 721

If you add this statement before showing alert dialog, you will not have to press Esc or Enter. This will release you from freezing your app.

alert.initModality(Modality.NONE);

Upvotes: 0

Related Questions