【问题标题】:Getting data object from the dialog in Vaadin从 Vaadin 的对话框中获取数据对象
【发布时间】:2022-11-05 07:51:52
【问题描述】:

我为我的 Vaadin 应用程序编写了一个扩展 Dialog 的类。 对话框面板包含几个文本字段、几个日期选择器等。 此对话框的目的是构建 Filter 类的实例。 这是一段代码。整个班级太大,无法在这里展示。

public class FilterPanel extends Dialog {

private Filter filter;

private TextField nameField;
private TextField countryField;
private DatePicker postingDateField;
private TextField fromYear;
private TextField toYear;
private ComboBox tagField;

public FilterPanel() {
    buildDialog();
}

public FilterPanel(Filter filter) {
    this.filter = filter;
}

一切正常,对象正在正确构建。 我的问题是我无法在关闭此对话框后将此对象移出此对话框。 我通过调用 close() 函数显式关闭它。 我将 Dialog.DialogCloseActionEvent 侦听器放入调用类,但它没有被调用。

    @Override
protected void onAttach(AttachEvent attachEvent) {
    super.onAttach(attachEvent); 
    registration = ComponentUtil.addListener(attachEvent.getUI(), DialogCloseActionEvent.class,
            event -> {
                Dialog source = event.getSource();
                if(source instanceof FilterPanel) {
                    reader = ((FilterPanel)source).getFilter();
                }
            });
}

我从按钮单击侦听器打开此对话框,并在显示对话框后尝试调用 getFilter() 方法。

        this.filterButton.addClickListener((ClickEvent<Button> clickEvent) -> {
        FilterPanel fp = config.getFilterPanel(filter);
        fp.open();
        filter = fp.getFilter();
    });

但是它总是返回 null。我在此行的调试器中放置了一个断点,它在对话框打开之前被调用。根据文档,默认情况下 Dialog 应该是模态的。 我如何使它工作?请帮帮我。

【问题讨论】:

  • 您能否显示创建对话框并添加关闭侦听器的代码?
  • 您确定要使用 Dialog.DialogCloseActionEvent 而不是 Dialog.OpenedChangedEvent?
  • 我在帖子中添加了代码。 @Tatu Lund:为什么 OpenedChangedEvent 更好?对话框关闭完成后,我确实需要取出数据。
  • 阅读 addDialogCloseActionListener 的 JavaDoc。该事件不会在 Dialog 关闭时触发,而是在用户点击对话框外部或按下 Esc 时触发,以便您自行决定是否关闭它。 OpenedChangedEvent 在 Dialog 打开和关闭时调度。
  • 我确实尝试在对话框外单击。事件也没有被触发。不确定我是否已正确注册该活动。我使用了食谱中的一个样本。我也在考虑使用类似于 Swing 的属性更改事件,但是 Vaadin 没有 PropertyChangeSupport 接口,因此我不知道如何使用它,也没有找到任何示例。

标签: event-handling dialog vaadin


【解决方案1】:

我确实尝试使用 Dialog.OpenedChangedEvent,但它没有按我的需要工作。 虽然 Dialog.DialogCloseActionEvent 根本没有被触发,但确实触发了但不是在正确的时刻。 最终,我编写了自己的 ComponentEvent 扩展,并在需要时显式触发它。它对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多