java javafx.scene.control.Dialog<R> 不会在按下“x”时关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32048348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:33:26  来源:igfitidea点击:

javafx.scene.control.Dialog<R> won't close on pressing "x"

javajavafxdialog

提问by Jonatan Stenbacka

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

如果我只是创建一个从 扩展的空类javafx.scene.control.Dialog<R>,当我按下右上角的“x”按钮时它不会关闭。

How do I implement this behaviour? The API seems to tell me that I need to implement a close button. But in my case I don't want a close button, I just want to close the window with the x button or by pressing ESC. Is this possible?

我如何实现这种行为? API 似乎告诉我,我需要实现一个关闭按钮。但就我而言,我不想要关闭按钮,我只想使用 x 按钮或按 ESC 关闭窗口。这可能吗?

回答by vbargl

The workaround from @eckig or @jewelsea works pretty fine. But I would use something like this:

@eckig 或 @jewelsea 的解决方法工作得很好。但我会使用这样的东西:

// Somewhere in code
Dialog<?> dialog = new Dialog<>();
Window    window = dialog.getDialogPane().getScene().getWindow();
window.setOnCloseRequest(event -> window.hide());

I do not know any constrains of this use, but it worked for me. And I recommend initialize window right after dialog initialization, like above.

我不知道这种用途的任何限制,但它对我有用。我建议在对话框初始化后立即初始化窗口,就像上面一样。

回答by jewelsea

To work-around this, you could add a hidden close button to the dialog.

要解决此问题,您可以向对话框添加一个隐藏的关闭按钮。

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class DialogClosure extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        Button openDialog = new Button("Open Dialog");
        openDialog.setOnAction(event -> {
            Dialog dialog = new Dialog();
            dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
            Node closeButton = dialog.getDialogPane().lookupButton(ButtonType.CLOSE);
            closeButton.managedProperty().bind(closeButton.visibleProperty());
            closeButton.setVisible(false);
            dialog.showAndWait();
        });

        stage.setScene(new Scene(openDialog));
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Then the dialog meets both your requirement of being able to be closed via the native windowing system's window close icon as well as the JavaFX Dialog requirement of including a close button in the dialog for the close icon to work.

然后,该对话框既满足您能够通过本机窗口系统的窗口关闭图标关闭的要求,也满足 JavaFX Dialog 在对话框中包含关闭按钮以使关闭图标起作用的要求。

Alternately, you could use a Stage with showAndWaitinstead of a Dialog. A Stage without any included buttons is closable using the windowing system's close window icon.

或者,您可以使用带有showAndWait的 Stage而不是Dialog。没有任何包含按钮的舞台可以使用窗口系统的关闭窗口图标关闭。

回答by eckig

To quote the Api Docs:

引用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.

    ...

JavaFX 对话框只能在两种情况下“异常”关闭(如上定义):

  1. 当对话框只有一个按钮时,或

  2. 当对话框有多个按钮时,只要其中一个满足以下要求之一即可:

    1. 该按钮有一个 ButtonType,其 ButtonData 的类型为 ButtonData.CANCEL_CLOSE。
    2. 该按钮有一个 ButtonType,其 ButtonData 在调用 ButtonData.isCancelButton() 时返回 true。

    ...

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

所以要么添加至少一个按钮或多个按钮,其中一个是 type ButtonData.CANCEL_CLOSE,例如:

Dialog<ButtonType> 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 FXDialogshown is HeavyweightDialogwhich is not public API so not really an extension point.

此行为在 中实现javafx.scene.control.FXDialog.requestPermissionToClose(Dialog<?>),但实际FXDialog显示的是HeavyweightDialog它不是公共 API,因此不是真正的扩展点。

回答by Anfuca

In my Dialog<ButtonType>I'm using what @vbargl said:

在我中,Dialog<ButtonType>我使用的是@vbargl 所说的:

Window window = alert.getDialogPane().getScene().getWindow();
window.setOnCloseRequest(event -> window.hide());

It closes the dialog, but it's bringing me a no value presenterror.

它关闭了对话框,但它给我带来了一个没有价值的错误。

To avoid it, I'm also checking result.get()and that result.isPresent().

为了避免它,我也在检查result.get()和那个result.isPresent()