未修饰的警报不会在请求时关闭
Undecorated Alert will not close when requested
我有一个简单的未修饰的 Alert
,我试图在后台完成一个长 运行 任务时显示它。
但是,当任务完成后,我想关闭警报。但是,我无法通过调用 close()
或 hide()
来关闭警报。
这个MCVE不包含后台Task
,但即使这样也拒绝关闭警报:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class AlertClosing extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Alert simpleAlert = new Alert(Alert.AlertType.NONE);
simpleAlert.setContentText("Testing");
simpleAlert.initStyle(StageStyle.UNDECORATED);
simpleAlert.show();
// None of these seem to have any effect.
simpleAlert.close();
simpleAlert.hide();
Platform.runLater(simpleAlert::close);
}
}
我在这里找到了一些其他答案,这些答案涉及具有取消按钮的 Alert
和 Dialog
窗格,但我的 Alert
根本没有按钮;它只是为了在后台任务 运行.
时显示一条消息
根据 Dialog
关闭规则中的 JavaDocs:
JavaFX dialogs can only be closed 'abnormally' (as defined above) in
two situations:
- When the dialog only has one button, or
- When the dialog has multiple buttons, as long as one of them meets one of the following requirements:
- The button has a ButtonType whose ButtonBar.ButtonData is of type ButtonBar.ButtonData.CANCEL_CLOSE.
- The button has a ButtonType whose ButtonBar.ButtonData returns true when ButtonBar.ButtonData.isCancelButton() is called.
In all other situations, the dialog will refuse to respond to all
close requests, remaining open until the user clicks on one of the
available buttons in the DialogPane area of the dialog.
所以这也表明应该尊重一个简单的 close()
请求,因为 Alert(AlertStyle.NONE)
中没有符合条件的按钮,对吗?
不,根据 javadoc 不遵守关闭请求:没有一个或多个按钮。
您可以分配一个 ButtonType
作为结果使 Alert
正常关闭。
Alert simpleAlert = new Alert(Alert.AlertType.NONE);
simpleAlert.setContentText("Testing");
simpleAlert.initStyle(StageStyle.UNDECORATED);
simpleAlert.show();
new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
Platform.runLater(() -> {
simpleAlert.setResult(ButtonType.CANCEL);
simpleAlert.close();
});
}).start();
我有一个简单的未修饰的 Alert
,我试图在后台完成一个长 运行 任务时显示它。
但是,当任务完成后,我想关闭警报。但是,我无法通过调用 close()
或 hide()
来关闭警报。
这个MCVE不包含后台Task
,但即使这样也拒绝关闭警报:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class AlertClosing extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Alert simpleAlert = new Alert(Alert.AlertType.NONE);
simpleAlert.setContentText("Testing");
simpleAlert.initStyle(StageStyle.UNDECORATED);
simpleAlert.show();
// None of these seem to have any effect.
simpleAlert.close();
simpleAlert.hide();
Platform.runLater(simpleAlert::close);
}
}
我在这里找到了一些其他答案,这些答案涉及具有取消按钮的 Alert
和 Dialog
窗格,但我的 Alert
根本没有按钮;它只是为了在后台任务 运行.
根据 Dialog
关闭规则中的 JavaDocs:
JavaFX dialogs can only be closed 'abnormally' (as defined above) in two situations:
- When the dialog only has one button, or
- When the dialog has multiple buttons, as long as one of them meets one of the following requirements:
- The button has a ButtonType whose ButtonBar.ButtonData is of type ButtonBar.ButtonData.CANCEL_CLOSE.
- The button has a ButtonType whose ButtonBar.ButtonData returns true when ButtonBar.ButtonData.isCancelButton() is called.
In all other situations, the dialog will refuse to respond to all close requests, remaining open until the user clicks on one of the available buttons in the DialogPane area of the dialog.
所以这也表明应该尊重一个简单的 close()
请求,因为 Alert(AlertStyle.NONE)
中没有符合条件的按钮,对吗?
不,根据 javadoc 不遵守关闭请求:没有一个或多个按钮。
您可以分配一个 ButtonType
作为结果使 Alert
正常关闭。
Alert simpleAlert = new Alert(Alert.AlertType.NONE);
simpleAlert.setContentText("Testing");
simpleAlert.initStyle(StageStyle.UNDECORATED);
simpleAlert.show();
new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
Platform.runLater(() -> {
simpleAlert.setResult(ButtonType.CANCEL);
simpleAlert.close();
});
}).start();