0

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?

user2856064
  • 541
  • 1
  • 8
  • 25

3 Answers3

2

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.

1

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);
Chris M
  • 1,058
  • 1
  • 15
  • 26
  • It's good, but I would like to remove window from inside the class, not from outside, using "this"? – user2856064 Mar 29 '17 at 11:06
  • You can call UI.getCurrent() from any class. – Chris M Mar 29 '17 at 11:07
  • You can use this loop from anywhere and it will remove all windows. – Chris M Mar 29 '17 at 11:10
  • I would like to do something like: UI.getCurrent().removeWindow(this); I don't want to remove all windows just one in which "I am in". – user2856064 Mar 29 '17 at 11:10
  • 1
    You can only do that if 'this' is your window – Chris M Mar 29 '17 at 11:11
  • I have got a class window and in this class there is a button and when user click this button the window should close/disappear/remove. Button has got a listener and in this function (listener) I need to add something like UI.getCurrent().removeWindow("this");. It is not good to remove all windows? – user2856064 Mar 29 '17 at 11:15
  • To sum up this is solution, but not efficient solution. – user2856064 Mar 29 '17 at 12:15
1

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.

Pschmeltz
  • 140
  • 1
  • 8