在 runLater 中关闭没有按钮的现有警报

Close an existing alert without button in runLater

我希望我的应用程序打开一个用户无法关闭的对话框,但是当他的 phone 重新连接时它会关闭(他对打开的对话框有耐心)

我目前正在使用 Platform.runlater 打开警告对话框,如下所示:

phoneConnected.getObservable().addListener(observable -> {
      LOG.info("identifyObs " + phoneConnected.getBooleanDataValue().getValue());
      if (phoneConnected.getBooleanDataValue() != null && !phoneConnected.getBooleanDataValue().getValue()) {
        Platform.runLater(() -> alert.show());
      }
      if (phoneConnected.getBooleanDataValue().getValue()) {
        System.out.println("ok");
        alert.close();
        Platform.runLater(() -> alert.close());
      }
    });

事实是,当 observable 变为 "true" 时,我在控制台中得到了 "ok",但对话框没有关闭..

我试过 runLater 和 runLater,有什么想法吗?

我想说,我看过这个: 但它不起作用..

如果未设置结果且没有可用按钮,似乎 Alert 无法正常关闭。您可以通过分配任意 ButtonType 作为结果来解决此问题:

Alert alert = new Alert(Alert.AlertType.NONE, "wait for it");

// set result to allow programmatic closing of alert
alert.setResult(ButtonType.OK);

Button btn = new Button("Start");
btn.setOnAction(evt -> {
    btn.setDisable(true);

    // make alert appear / disappear
    Thread t = new Thread(() -> {
        boolean showing = false;
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(NewFXMain2.class.getName()).log(Level.SEVERE, null, ex);
            }
            Runnable action = showing ? alert::close : alert::show;
            Platform.runLater(action);
            showing = !showing;
        }
    });
    t.setDaemon(true);
    t.start();
});