Loading...
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.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Disable Tree Item

Thara Mediacept
1 decade ago Mar 19, 2010 3:06pm
Marko Grönroos
1 decade ago Mar 21, 2010 9:35pm

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

Thara Mediacept
1 decade ago Mar 22, 2010 1:08pm
Jani Laakso
10 years ago Nov 01, 2011 11:31pm
Ranjit reddy
7 years ago Oct 30, 2014 6:05am
Joacim Päivärinne
7 years ago Oct 30, 2014 6:44am
Tugrul Duyar
2 years ago May 13, 2019 1:12pm