Monday Oct 13, 2008

World's Simplest NetBeans Node Implementation

While looking into some small corners of the Nodes API, I realized that this is probably the simplest Nodes implementation. It is perfect for demos and presentations and trainings, etc. Anyone can code this and it is easy to explain what's going on:
class SystemPropertyNode extends Children.Keys {

    protected void addNotify() {
        Properties props = System.getProperties();

    protected Node[] createNodes(String key) {
        return new Node[]{new PropNode(key)};

    private class PropNode extends AbstractNode {
        private PropNode(String key) {
            super(Children.LEAF, Lookups.fixed());


Once you've got that, you need this in your view component (which could be a TopComponent or a JFrame):

ExplorerManager.setRootContext(new AbstractNode(new SystemPropertyNode()));

In addition, you'd need to implement ExplorerManager.Provider and add an explorer view. But most of that part can be done via the ui in the IDE. Short, simple, nice demo of Nodes API, ending with you swapping different explorer views around to show that the model is disconnected from the view.

Update 07/23/14: Go here for an updated implementation of the above together with a TopComponent that contains it.

How Users Can Let Children Move Up & Move Down (Part 1)

"Move up" and "Move down" support for nodes is provided by the NetBeans Platform, as described in this "NetBeans Definitive Guide" page on-line. I tried to get it to work for some of my child nodes today and managed to get it working, with Jarda's help. The menu items are enabled, if I select one child node at a time, and they work as one would expect them to:

Let's say I define my children like this:

public class PropChildren extends Index.ArrayChildren {

    protected java.util.List initCollection() {
        ArrayList childrenNodes = new ArrayList();
        Properties p = System.getProperties();
        Enumeration e = p.keys();
        while (e.hasMoreElements()) {
            childrenNodes.add(new PropNode(e.nextElement().toString()));
        return childrenNodes;

    public class PropNode extends AbstractNode {

        public PropNode(String key) {
            super(Children.LEAF, Lookups.fixed());

        //You need to add the 'MoveUpAction' and 'MoveDownAction',
        //which are part of the Actions API:
        public Action[] getActions(boolean popup) {
            return new Action[]{

For the above, you need the following in the TopComponent's constructor (of course, the TopComponent implements ExplorerManager.Provider and has an explorer view added to it):

//Create an instance of the children class:
PropChildren prop = new PropChildren();

//Add the map of actions to the TopComponent's Lookup:
associateLookup(ExplorerUtils.createLookup(explorerManager, getActionMap()));

//Create the children and
//include an implementation of the Index class in the Lookup of the root node.
//Since our children class above is such an implementation, we'll include that:
explorerManager.setRootContext(new AbstractNode(prop, Lookups.singleton(prop)));

//Set the display name of the root node:

The key to the above is that I have extended Index.ArrayChildren, which provides me with reorderable children out of the box (but only if I include the class in the Lookup of the root node, as shown above). If you're using Children.Keys or ChildFactory, the children class could implement Index. Or you could create a new class that extends Index.Support, which should then be added to the Lookup as shown above. But then you would have to handle the reordering yourself, unlike with Index.ArrayChildren which, as can be seen above, gives you that functionality for free.


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.


« October 2008 »