Why do my nodes in the Explorer always have an expand-box by them, even though they have no children?

Apache NetBeans Wiki Index

Note: These pages are being reviewed.

Nodes are not asked for their child nodes until the user tries to expand them - to do otherwise would be very bad for performance. If your Node is not supposed to have child nodes, use Children.LEAF as the children object passed to the constructor. That will eliminate the expand handle.

Remove the expander, if you know the parent-/child-hierarchy at creation time

(Taken from platform-dev mailinglist - author Mark J.Koch - mark@maehem.com)

In my case, our Nodes are backed by actual objects that know their own parent/child hierarchy. First we create our Nodes always as LEAF nodes by default and then set the children (if there are children) later. I have a method in my Node that get’s called whenever our data model changes. First we check if we are already a LEAF node, and if we are, and we have children, we simply initialize the children. If we already have children, we check if our current data model has children here and either update our children keys or explicitly set Children.LEAF if there are no children.

This will properly make the expander disappear when there are no children.

void myModelChanged() {
    if (this.isLeaf()) {
        if (getObject().hasChildren()) {
            initChildren();
        }
    } else {
        if (getObject().hasChildren()) {
            // Cause children to regenerate it's key list.
            this.getChildren().addNotify();
        } else {
            // We no longer have children. Change to leaf.
            setChildren(Children.LEAF);
        }
    }
}