Saturday May 25, 2013

No Expansion Icon When No Children (Part 2)

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:


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« May 2013 »