在Vaadin中打开新对话框时关闭对话框

在Vaadin中打开新对话框时关闭对话框,vaadin,Vaadin,我在某个UI中创建了一个对话框 Dialog dialog = new Dialog(); dia.add(new TestView()); dia.open(); 新的UI TestView包含一个按钮,该按钮再次创建一个新对话框 private void button_onClick(ClickEvent<Button> event { Dialog dialog = new Dialog(); dia.add(new TestView2()); dia

我在某个UI中创建了一个对话框

Dialog dialog = new Dialog();
dia.add(new TestView());
dia.open();
新的UI TestView包含一个按钮,该按钮再次创建一个新对话框

private void button_onClick(ClickEvent<Button> event {
    Dialog dialog = new Dialog();
    dia.add(new TestView2());
    dia.open();
    ...
}
创建第二个对话框时,如何关闭第一个对话框

Dialogthis.getParent.close;这是不可能的

为了更好地理解:


当UiTwo创建UiThree时,我希望UiTwo被关闭。因此,始终只有一个对话框打开。

这可能适用于您:

公共静态类CustomDialog扩展对话框{ 私人对话家长; 公共自定义对话框父级{ this.parent=parent; } @凌驾 公共空间开放{ 超级开放; 如果父级!=null{ parent.close; } } } //用法: 公共主视图{ CustomDialog parentDialog=新建CustomDialognull; parentDialog.addnew按钮打开子项,e->{ CustomDialog childDialog=新建CustomDialogparentDialog; //将某些内容设置为子对话框 childDialog.open; }; parentDialog.open; //...
您的方法实际上是可行的,但请注意,getParen返回的是可选的,而不是组件。因此,您必须执行以下操作:

getParent().ifPresent(parent -> {
    if (parent instanceof Dialog) {
        ((Dialog) parent).close();
    }
});
或者如果你很勇敢

((Dialog) getParent().get()).close();
如果您想以参考方式进行,这是一种方式:

@Route
public class MainView extends VerticalLayout {

    public MainView() {
        Dialog dialog = new Dialog();
        dialog.add(new DialogView(dialog));
        dialog.open();
    }
}
还有你的TestView2

您甚至不必关闭第一个对话框,只需替换内容即可

public class DialogView extends VerticalLayout {

    public DialogView(Dialog dialog) {
        Button button = new Button("Next step");
        button.addClickListener(e -> {
            dialog.removeAll();
            dialog.add(new Span("You are in the third step"));
        });
        add(button);
    }
}

如果您有许多视图需要这样循环,我将创建一个抽象类,实现所有视图之间的通用功能。

您可以将当前使用的对话框的引用保存在字段中,并在重新指定打开新对话框时将其关闭it@JuliusHörger你能举个例子怎么做吗?难道没有直接访问在上面UI中创建的对话框以关闭它的方法?默认情况下,对话框不知道其他对话框。该示例提供了一种方法。我的主UI没有对话框。我只想从一个对话框中获取属性。我不确定我是否理解正确,但至少我的想法是正确的使用上述普通Java编程技术在对象之间传递信息。
public class DialogView extends VerticalLayout {

    public DialogView(Dialog dialog) {
        Button button = new Button("Next step");
        button.addClickListener(e -> {
            dialog.removeAll();
            dialog.add(new Span("You are in the third step"));
        });
        add(button);
    }
}