You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在runLater中无按钮关闭弹窗并实现手机重连自动关闭不可关闭对话框

Solution for Your JavaFX Dialog Requirements

1. Programmatically Close an Alert in Platform.runLater

To close an existing Alert without relying on user-clicked buttons, you need to retain a reference to the Alert instance so you can call its close() method when needed. Here’s how to integrate this with your existing phone connection listener:

First, declare a class-level variable to hold the Alert:

private Alert connectionLostAlert;

Then update your listener logic to manage the alert’s lifecycle:

phoneConnected.getObservable().addListener((observable, oldValue, newValue) -> {
    LOG.info("identifyObs " + newValue);
    if (newValue != null && !newValue) {
        Platform.runLater(() -> {
            // Only create/show the alert if it doesn't already exist
            if (connectionLostAlert == null || !connectionLostAlert.isShowing()) {
                connectionLostAlert = new Alert(Alert.AlertType.WARNING);
                connectionLostAlert.setTitle("Connection Lost");
                connectionLostAlert.setHeaderText(null);
                connectionLostAlert.setContentText("Your phone has disconnected. Waiting for reconnection...");
                
                // Remove default buttons to prevent manual closure (ties into your second requirement)
                connectionLostAlert.getButtonTypes().clear();
                
                connectionLostAlert.show();
            }
        });
    } else {
        // When phone reconnects, close the alert safely on the FX thread
        Platform.runLater(() -> {
            if (connectionLostAlert != null && connectionLostAlert.isShowing()) {
                connectionLostAlert.close();
                // Reset reference to allow a new alert on future disconnections
                connectionLostAlert = null;
            }
        });
    }
});

Key details:

  • We avoid duplicate alerts by checking if one is already showing.
  • All UI actions are wrapped in Platform.runLater to ensure they execute on the JavaFX application thread.

2. Create a Non-Manually Closable Dialog That Auto-Closes on Reconnection

To block users from manually closing the dialog (via window X button, Alt+F4, etc.), we’ll consume the close request event unless the phone has reconnected. Modify the alert creation code with this handler:

connectionLostAlert = new Alert(Alert.AlertType.WARNING);
connectionLostAlert.setTitle("Connection Lost");
connectionLostAlert.setHeaderText(null);
connectionLostAlert.setContentText("Your phone has disconnected. Waiting for reconnection...");

// Remove default buttons
connectionLostAlert.getButtonTypes().clear();

// Prevent manual closure by consuming the close request
connectionLostAlert.setOnCloseRequest(event -> {
    // Only allow closing if the phone is reconnected
    Boolean isConnected = phoneConnected.getBooleanDataValue() != null ? phoneConnected.getBooleanDataValue().getValue() : false;
    if (!isConnected) {
        event.consume(); // Block the user's close attempt
    }
});

connectionLostAlert.show();

Now the dialog can only be closed programmatically when your listener detects a reconnection—users can’t dismiss it themselves.

Quick Tips

  • Always wrap UI operations in Platform.runLater when triggered from background threads (your connection listener likely runs off the FX thread).
  • Resetting the connectionLostAlert reference after closing ensures a fresh alert will appear if the phone disconnects again later.

内容的提问来源于stack exchange,提问作者sleakerz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:13:45