如何在runLater中无按钮关闭弹窗并实现手机重连自动关闭不可关闭对话框
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.runLaterto 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.runLaterwhen triggered from background threads (your connection listener likely runs off the FX thread). - Resetting the
connectionLostAlertreference after closing ensures a fresh alert will appear if the phone disconnects again later.
内容的提问来源于stack exchange,提问作者sleakerz

