Tree: removing child nodes of a parent

I’m new to vaadin and I’m looking for a way to remove all the child nodes. I tried

@Override
public void nodeCollapse(CollapseEvent event) {
    Object o=event.getItemId();
    
    Collection<?> col=tree.getChildren(o);
    col.clear();
}

and received an UnsupportedOperationException.

I tried using an Iterator and removing one at a time and also received an error. I looked through the forums and couldn’t find the answer. Anyone have a solution to this?

Thanks in advance.

Hi,

If you are using Java 8, you can try something like:

Collection<?> childrenIDs = tree.getChildren(o);
childrenIDs.stream().forEach(tree::removeItem);

If not, maybe something like:

[code]
Object toRemove = childrenIDs.toArray();
int size = toRemove.length;

for (int i = 0; i < size; i++) {
tree.removeItem(toRemove[i]
);
}
[/code]Hope that helps!