Geertjan's Blog

  • July 22, 2014

World's Simplest NetBeans Node/TopComponent Implementation

Geertjan Wielenga
Product Manager

In 2008, here, I documented the world's simplest NetBeans Node implementation. Here's the updated implementation, using ChildFactory, together with the code needed for connecting it into a TopComponent:

public final class ViewerTopComponent extends TopComponent implements ExplorerManager.Provider {
    private ExplorerManager em = new ExplorerManager();
    public ViewerTopComponent() {
        setLayout(new BorderLayout());
        add(new BeanTreeView(), BorderLayout.CENTER);
        Children kids = Children.create(new SystemPropertyChildFactory(), true);
        AbstractNode rootNode = new AbstractNode(kids);
        associateLookup(ExplorerUtils.createLookup(em, getActionMap()));
    public ExplorerManager getExplorerManager() {
        return em;
    class SystemPropertyChildFactory extends ChildFactory<Object> {
        protected boolean createKeys(List<Object> list) {
            return true;
        protected Node createNodeForKey(Object key) {
            BeanNode node = null;
            try {
                node = new BeanNode(key);
            } catch (IntrospectionException ex) {
            return node;

Here are the class annotations for registering the above in the Window System:

preferredID = "ViewerTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
mode = "explorer",
openAtStartup = true)
category = "Window",
id = "org.demo.viewer.ViewerTopComponent")
path = "Menu/Window" /*, position = 333 */)
displayName = "#CTL_ViewerAction",
preferredID = "ViewerTopComponent"
"CTL_ViewerTopComponent=Viewer Window"

And here are the import statements:

import java.awt.BorderLayout;
import java.beans.IntrospectionException;
import java.util.List;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

Join the discussion

Comments ( 3 )
  • nkoding Thursday, September 11, 2014

    Hi Geertjan,

    i tried this code and get an java.lang.IllegalStateException when calling accociateLookup in the TopComponent's constructor.

    i'm using NetBeans Platform 8.

    Is this already known? i found some info on that error but no solutions. Could you please help?



  • Geertjan Thursday, September 11, 2014

    It's not known because it's not true. Send your code in a ZIP file to geertjan dot wielenga at oracle dot com and I'll try to fix your code.

  • guest Thursday, September 11, 2014

    I found my mistake. I entered the wrong ID in the action registration annotation.

    thanks for your reply and efforts!



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