Geertjan's Blog

  • May 25, 2013

No Expansion Icon When No Children (Part 2)

Geertjan Wielenga
Product Manager

In part 1, the problem of the superfluous expansion icon is discussed and solved. However, the solution is incomplete as Michael Bishop indicates in the comment to part 1:

I recently implemented this solution. The explorer tree appeared
correctly, but no longer updates correctly; adding and removing nodes
don't seem to be triggering the refresh() method in the factory class.
For instance, when adding a new child, the underlying data model updates
and I can confirm the child is added, but nothing happens in the tree.

The solution to the above is to (1) detect when a new child is added and to (2) then call Node.setChildren to update the node hierarchy. Set a PropertyChangeListener on the node and then call setChildren when you detect that a new child has been added.

public void propertyChange(PropertyChangeEvent evt) {
    if (evt.getPropertyName().equals("ADD")) {
        setChildren(Children.create(new ObjectChildFactory(bean), false));

This technique lets you show the user the true hierarchical state of each node, i.e., only if a node has children is an expansion icon shown.

The whole solution, i.e., a complete application that shows all the pieces fitted together, is available here:


Join the discussion

Comments ( 3 )
  • Michael Bishop Monday, May 27, 2013

    It might be worth noting that the Callable class determines whether or not there are children. A more robust solution might detect a "children changed" event and call setChildren using the Callable (as done in the original constructor to the node).

    Otherwise, things might start to fall apart on "remove" events since you have to determine if there are any children left and if so, use Children.LEAF.

    In fact, I think you have to call setChildren() in only two cases ideally:

    - When going from 0 to 1+ children. (Setting a factory)

    - When going from 1+ children to 0 children. (Setting a leaf)

    I'll be working with this in my project and will try to update the sample project accordingly.

  • Geertjan Monday, May 27, 2013

    Great! Yes, make changes to the sample project as needed.

  • Ashish Kirpan Tuesday, July 21, 2015

    Hi Geertjan,

    I am using outline view, where I am using ChildFactory.Detachable to create nodes.

    I am adding and removing nodes dynamically by using PropertyChangeListener as mentioned above.

    While adding node it is working fine. But while removing node, I am calling ChildFactory.refresh() method, but it is not creating nodes again and not removing expansion icon. Expansion icon appears even though there is no child in node.

    I want to know where my code goes wrong?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.