Select child


First : well done with Vaadin, it’s the greatest RIA framework !

I got a Tree with a HierarchicalContainer.
I want to do this stuff : when I click an “item” of my tree, I would like to select (or unselect) all its childs.

I try this :

hierarchie.addListener(new ItemClickListener() {
	public void itemClick(ItemClickEvent event) {
		Object itemId = event.getItemId();
		Collection coll = hierarchie.getChildren(itemId);
		if(coll != null)
			for (Object i : hierarchie.getChildren(itemId));

But the childs don’t select on screen…


I think you could simply say:


The tree has to be in multi-select mode: setMultiSelect(true). In that mode, the value property of the Tree is a collection of the selected item ids. A bit like

Ok, it’s look good but…

When I debug :

for(Object cat : (Set<Object>) hierarchie.getValue()) {

cat is an instance of IndexedContainer$IndexedContainerItem (id=3474)
with itemId Categorie (id=3504)
and this$0 GwladysHierarchieContainer (id=3505)

How to access / get my Categorie object?


You always set and get the selection in terms of item identifiers. In particular, the selection should never be an Item unless your item identifiers are also (other) Items, which would be a very exceptional use case.

If you have set the multiselect mode as Marko suggested, the return value should always be a Set of item ids.

The question is, how did you add the items to your container? If you did give your Categorie as an item id, it should be what you get in the set. If you did not give an id to addItem(), the identifiers are automatically generated, and the Categorie instance does not exist in the container unless you explicitly put it there as a property of the item.

Could it be that you have (bypassing the related checks by calling setValueInternal()) set the value to be some Item, not an item identifier?

You can also see
this thread
where Alexander Pchelintsev has adapted BeanItemContainer to implement Container.Hierarchical.

Ok, I understand that my Categorie object are ids not item.

Last question, here my code :

hierarchie.addListener(new ItemClickListener() {
	public void itemClick(ItemClickEvent event) {
		Categorie cat = (Categorie) event.getItemId();
		if (!hierarchie.isSelected(cat) && hierarchie.getChildren(cat) != null) {
			for(Object id : hierarchie.getChildren(cat));
		} else if (hierarchie.getChildren(cat) != null){
			for(Object id : hierarchie.getChildren(cat))

My tree is useed to filter my product tables. With that code it work but the childs items are not selected (in blue).
Is there a solution to this?


Have you tried debugging the code to see what it does on runtime?

I have a little hard following the unknown data types and methods that I don’t know what they do (refresh?), but I’ll give it a shot.

if (!hierarchie.isSelected(cat) && hierarchie.getChildren(cat) != null) {

turned into English: if the item is NOT selected and item has children. Shouldn’t the first ‘!’ be removed? If I understand that correctly, it will always go to the ‘else’ part.

I should select and unselected child when the user click the parent.

And when I enter public void itemClick(ItemClickEvent event) method, the select state of the parent is not yet changed.
So the first “!” is in the right place. (for me)

But my problem is on screen : clichs items are not selected (in blue).