javafx.scene.control.Dialog won't close on pressing “x”

前端 未结 5 1036
傲寒
傲寒 2020-12-06 17:36

If I just create an empty class extending from javafx.scene.control.Dialog, it won\'t close when I\'m pressing the \"x\" button in the top right corner

5条回答
  •  萌比男神i
    2020-12-06 18:17

    To quote the Api Docs:

    JavaFX dialogs can only be closed 'abnormally' (as defined above) in two situations:

    1. When the dialog only has one button, or

    2. When the dialog has multiple buttons, as long as one of them meets one of the following requirements:

      1. The button has a ButtonType whose ButtonData is of type ButtonData.CANCEL_CLOSE.
      2. The button has a ButtonType whose ButtonData returns true when ButtonData.isCancelButton() is called.

      ...

    So either add at least one button or multiple buttons, and one of them is of type ButtonData.CANCEL_CLOSE, for example:

    Dialog dialog = new Dialog<>();
    dialog.getDialogPane().getButtonTypes().add(new ButtonType("Got it!", ButtonData.CANCEL_CLOSE));
    dialog.setContentText("test");
    dialog.showAndWait();
    

    Edit:

    This behavior is implemented in javafx.scene.control.FXDialog.requestPermissionToClose(Dialog), but the real FXDialog shown is HeavyweightDialog which is not public API so not really an extension point.

提交回复
热议问题