【问题标题】:Javafx Dialog (Alert) not visible if owner is minimized如果所有者最小化,Javafx 对话框(警报)不可见
【发布时间】:2016-04-28 09:30:15
【问题描述】:

如果所有者被最小化,为什么 javafx 中的对话框(警报)不能正确显示。

看下面的代码:

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);
}
}

如果您在 2 秒内最小化应用程序,然后再次最大化它(2 秒后),您不会看到对话框,但它以某种方式存在(舞台被锁定,直到您按 esc 或 enter)

如果您不最小化阶段,对话框将正确显示。

这是一个错误吗?我做错了吗?

编辑: 系统是Windows 7,Java 1.8.0.66

编辑2: 行。看起来它真的是一个错误: https://bugs.openjdk.java.net/browse/JDK-8151170

【问题讨论】:

    标签: java javafx dialog javafx-8


    【解决方案1】:

    找到了(可能的)解决方案。 但这真的是一个好的解决方案吗?

    在显示警报之前,我执行以下行:

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

    如果有人有更好的想法,我会删除我的答案。

    【讨论】:

      【解决方案2】:

      这里是静态方法,嵌入@Lurking Elk之前的提示。

      它会自动执行该过程,然后将父级恢复到其当前的“图标化”状态。

      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;
      }
      

      【讨论】:

        【解决方案3】:

        如果您在显示警报对话框之前添加此语句,则不必按EscEnter。这将使您免于冻结您的应用程序。

        alert.initModality(Modality.NONE);
        

        【讨论】:

        • 是的,我现在不必按 esc 或输入。但是对话框也没有显示。所以答案不正确
        【解决方案4】:
        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());
        }
        

        我无法理解它以前和现在都有效,停止。 Oracle jdk1.8.0_111 linux机器

        EDIT:我发现了问题,JVM 参数 -Dcom.sun.javafx.isEmbedded=true 如果没有这个选项,它就可以工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多