Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Disable Tree Item
Hi,
I want to disable items of tree, I use this:
Tree.setEnabled(false);
that disable all tree nodes and items.
but How can I disable just some items of tree component ?
There isn't a built-in way to "disable" tree nodes. You can somewhat easily prevent selecting "disabled" items by reverting any disallowed selection to the previous selection. This requires that you set the tree in immediate mode though. You can also style tree nodes to look as "disabled":
final Tree tree = new Tree("My Tree");
tree.setContainerDataSource(createTreeContent());
// Show all leaf nodes as disabled
tree.setItemStyleGenerator(new Tree.ItemStyleGenerator() {
public String getStyle(Object itemId) {
if (! tree.hasChildren(itemId))
return "disabled";
return null;
}
});
// Disallow selecting "disabled" leaf nodes
tree.addListener(new Property.ValueChangeListener() {
Object previous = null;
public void valueChange(ValueChangeEvent event) {
if (! tree.hasChildren(tree.getValue()))
tree.setValue(previous);
else
previous = tree.getValue();
}
});
tree.setImmediate(true);
layout.addComponent(tree);
And the CSS:
.v-tree-node-caption-disabled {
color: graytext;
font-style: italic;
}
See a live example
Hi,
I use:
tree.setItemStyleGenerator(new Tree.ItemStyleGenerator() {
public String getStyle(Object itemId) {
if (! tree.hasChildren(itemId))
return "disabled";
return null;
}
});
and it is very helpfull ! thanks
Nice trick. Note that end users may still tamper with the messages being sent from the client-side (browser) into server-side and actually select nodes that you expect not to be ever selected. If you are strict about logic / security, you might want to check that only correct values have been selected.
Hi,
I have a tree with items that get store after button click into the panel and i have a combobox with some four values if i select specific second value from ComboBox and press the Button then only it has to show the Tree item in the panel otherwise it should not show the item at all how i can achieve this can u please help me regarding this
Hey Ranjit,
Are you talking about filtering? Perhaps this might help: https://vaadin.com/forum/#!/thread/264195/264194.
Henri Sara: Filtering is related to the container. If you are using HierarchicalContainer (which does have filtering support), see the javadoc for Container.Filterable and HierarchicalContainer.
Here's an example: https://dev.vaadin.com/svn/doc/book-examples/trunk/src/com/vaadin/book/examples/datamodel/ContainerFilterExample.java.
Hi,
its been 5 years since last one replied here.
is there still the problem with disabling items on a Vaadin (8) Tree? i mean beside the workaround Marko Grönroos mentioned.