首页 文章

JavaFX Dialog一遍又一遍地返回相同的结果

提问于
浏览
1

解决:我认为它不是想要将它保存到变量中 . 要刷新缓存,我在对话框中使用了setResult(null) . 那对我来说很棒:)

我已经创建了一个Dialog来使用JavaFX编辑/创建Person . 但我想我发现了一个错误 . 我在对话框中添加了两个ButtonType对象 . 一个用于保存,一个用于中止操作 . 当我使用这些按钮时它工作得很好 . 但是,如果按“X”关闭对话框窗口,对话框将自动再次返回最后一个结果 . 这意味着如果我中止了我的最后一个操作,并且在我当前的操作中按“X”关闭窗口,则对话框不返回任何结果,因为中止按钮没有最后一次 . 但是如果我在我的最后一个动作上按下保存按钮并且我在当前动作中按下“X”它再次返回同一个人,因为保存按钮上次在其结果中有这个人 . 如何在关闭时将对话框更改为无结果?

这是我创建的动作:

this.createPersonAction = new Callback<ButtonType, PersonSession>() {

        @Override
        public PersonSession call(final ButtonType param) {
            if (param.equals(PersonDialogController.this.saveButton)) {
                final String firstName = PersonDialogController.this.firstNameField.getText();
                final String lastName = PersonDialogController.this.lastNameField.getText();
                final Person p = BeanFactory.createPerson(firstName, lastName);
                if (p != null) {
                    return new PersonSession(p);
                }
            }
            return null;
        }
    };

在这里你有我的两个ButtonType-Objects:

private final ButtonType saveButton = new ButtonType(GuiStringRresource.LABEL_SAVE_BUTTON, ButtonData.OK_DONE);
private final ButtonType abortButton = new ButtonType(GuiStringRresource.LABEL_ABORT_BUTTON,
        ButtonData.CANCEL_CLOSE);

1 回答

  • 1

    使用 Dialog.setOnCloseRequest 方法来处理这种情况 .

相关问题