Vaadin - How add checkbox component to a tree?
Asked Answered
C

2

5

I'm using Vaadin 7.5.3 to develop a web application. There I need a tree with selectable nodes. I want to select nodes using checkboxes. After trying many ways and goggling, I could not find how to add CheckBox component to a tree node.

Curtsy answered 21/9, 2015 at 4:59 Comment(2)
What have you so far? (Have you tried in the tree editable mode)?Tb
What do you mean by editable mode? Do you mean tree.setReadOnly(false)?Curtsy
P
6

As per my knowledge, up to the current latest version aka Vaadin 7.5.6, this is not possible, as Jouni point out in this discussion on their forums. He has also opened an improvement ticket but I don't see any changes so far.

Nonetheless, you should be able to fake it by using the TreeTable component. You can find here a complete example, and below an excerpt from it:

final TreeTable ttable = new TreeTable("My TreeTable");
ttable.addContainerProperty("Name", CheckBox.class, "");
ttable.addContainerProperty("City", String.class, "");
ttable.setWidth("20em");

// Create the tree nodes
ttable.addItem(new Object[]{new CheckBox("Root"), "Helsinki"}, 0);
ttable.addItem(new Object[]{new CheckBox("Branch 1"), "Tampere"}, 1);
ttable.addItem(new Object[]{new CheckBox("Branch 2"), "Turku"}, 2);
ttable.addItem(new Object[]{new CheckBox("Leaf 1"), "Piikkiö"}, 3);
ttable.addItem(new Object[]{new CheckBox("Leaf 2"), "Parainen"}, 4);
ttable.addItem(new Object[]{new CheckBox("Leaf 3"), "Raisio"}, 5);
ttable.addItem(new Object[]{new CheckBox("Leaf 4"), "Naantali"}, 6);

// Set the hierarchy
ttable.setParent(1, 0);
ttable.setParent(2, 0);
ttable.setParent(3, 1);
ttable.setParent(4, 1);
ttable.setParent(5, 2);
ttable.setParent(6, 2);

// Expand the tree
ttable.setCollapsed(2, false);
for (Object itemId: ttable.getItemIds())
    ttable.setCollapsed(itemId, false);

ttable.setPageLength(ttable.size());

And this is the output

checkbox in tree

Paphos answered 23/9, 2015 at 12:4 Comment(0)
D
1

This is an old question, so for what it's worth: the Vaadin demo page has an example of exactly this:

http://demo.vaadin.com/book-examples/book#component.tree.itemstylegenerato

Drawl answered 31/1, 2017 at 20:50 Comment(3)
github.com/vaadin/book-examples/blob/master/src/com/vaadin/book/…Obregon
Why not to use TreeGrid?Obregon
@Obregon that didn't exist at the timeDrawl

© 2022 - 2024 — McMap. All rights reserved.