How to close window in vaadin?

2019-08-19 00:33发布

问题:

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.