Window Close

It’s possible to close all opened windows?

I try to use this when I will change my view, so every time I change e view I need to close any window opened:

Collection<Window> windowList = UI.getCurrent().getWindows();
for(Window www : windowList){
	www.close();
}

But I got this error:

java.util.ConcurrentModificationException: null
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719) ~[na:1.8.0_162]

at java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:742) ~[na:1.8.0_162]

This is most likely happening because when window is closed it is removed from the Map backing UI.getCurrent().getWindows();, breaking the iterator.

This can be worked around (in a dirty way :wink: with:

Collection<Window> windowList = new ArrayList<>(UI.getCurrent().getWindows());
for(Window www : windowList){
	www.close();
}

In this case you’ll have a copy of window list, and window.close() will not affect it.

Artem Godin I already try this and get the same error

What is the context where you call it? I tried Artems solution and it worked (encapsulating it in another list).

Maybe try it with an array or an iterator instead.

Array

Collection<Window> windows = UI.getCurrent().getWindows();

Object[] array = windows.toArray();
for (int i = 0; i < array.length; i++) {
	((Window)array[i]
).close();
}

Iterator

Iterator<Window> windowList = new ArrayList<>(UI.getCurrent().getWindows()).iterator();
while (windowList.hasNext()) {
	Window next = windowList.next();
	next.close();
}