user2856064
user2856064

Reputation: 553

How to close window in vaadin?

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?

Upvotes: 0

Views: 5767

Answers (3)

Pschmeltz
Pschmeltz

Reputation: 150

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.

Upvotes: 1

Farhan Nazmul
Farhan Nazmul

Reputation: 51

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.

Upvotes: 2

Chris M
Chris M

Reputation: 1068

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);

Upvotes: 1

Related Questions