How to close window in vaadin?

岁酱吖の 提交于 2019-12-12 06:22:46

问题


I have got class MyCustomWindow which extends Window (com.vaadin.ui) from vaadin. When you click some button MyCustomWindow will show. Now I would like to add button to this window and when you push this buton it will close the window. I have got problem what to use to remove this window. I have found:

Window w = getWindow();
getApplication().removeWindow(w);

or

Window w = this.findAncestor(Window.class);
w.close();

But it doesn't work. I would like to remove window from inside the class, not from outside, using "this"? Something like:

UI.getCurrent().removeWindow(this);

I am using vaadin 7. Can you help me?


回答1:


Hello if you want to close the window from inside your click listener you can do one of the following two things:

yourButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                MyCustomWindow.this.close();
            }
        });

Or:

yourButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                closeMyCustomWindow();
            }
        });

private void closeMyCustomWindow(){
   this.close();
}

closeMyCustomWindow() this function is inside the MyCustomWindow class.




回答2:


You could use this code to remove all windows.

for (Window window : UI.getCurrent().getWindows())
        {

            UI.getCurrent().removeWindow(window);
            window.close();
        }

However if you already have reference to the window all you need is this:

UI.getCurrent().removeWindow(window);



回答3:


You can not modify windows while iterating. Copy the collection first.

for (Window window : new ArrayList<>(UI.getCurrent().getWindows())){
   window.close();
}

Removing windows while iterating on getWindows will throw concurrent modification exception.



来源:https://stackoverflow.com/questions/43090965/how-to-close-window-in-vaadin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!