按下&#34; x&#34; javafx.scene.control.Dialog <r>不会关闭

时间:2015-08-17 10:41:27

标签: java javafx dialog

如果我只创建一个从javafx.scene.control.Dialog<R>延伸的空类,当我按下&#34; x&#34;时,它就不会关闭。右上角的按钮。

如何实现此行为? API似乎告诉我,我需要实现一个关闭按钮。但在我的情况下,我不想要一个关闭按钮,我只想用x按钮关闭窗口或按ESC。这可能吗?

5 个答案:

答案 0 :(得分:16)

来自@eckig或@jewelsea的解决方法非常好用。但我会用这样的东西:

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

我不知道这种用法的任何限制,但它对我有用。 我建议在对话框初始化后立即初始化窗口,如上所述。

答案 1 :(得分:10)

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

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);
    }
}

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

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

答案 2 :(得分:8)

引用Api Docs

  

JavaFX对话框只能在“异常”(如上所述)中关闭   两种情况:

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

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

         
        
    1. 该按钮具有ButtonType,其ButtonData   是ButtonData.CANCEL_CLOSE类型。
    2.   
    3. 该按钮的ButtonType为   调用ButtonData.isCancelButton()时,ButtonData返回true。
    4.         

      ...

        

因此,要么添加至少一个按钮,要么添加多个按钮,其中一个按钮类型为ButtonData.CANCEL_CLOSE,例如:

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

编辑:

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

答案 3 :(得分:1)

Dialog<ButtonType>我使用@vbargl所说的内容:

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

它会关闭对话框,但它会为我带来 no value present 错误。

为了避免这种情况,我还要检查result.get()result.isPresent()

答案 4 :(得分:0)

面对这种情况,我会使用以下代码发出警报。

Alert alert = new Alert(Alert.AlertType.NONE);
     alert.setTitle("Export Successful");
     alert.setContentText("All the posts have been exported to Posts.txt file 
 successfully");
    alert.show(); 

唯一的解决方法是创建一个警报或对话框,其中带有一个按钮,如下所示。

Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setTitle("Export Successful");
    alert.setContentText("All the posts have been exported to Posts.txt file 
successfully");
    alert.showAndWait();
相关问题