Over a million developers have joined DZone.

How to Create a NetBeans Platform Explorer View

DZone 's Guide to

How to Create a NetBeans Platform Explorer View

· Java Zone ·
Free Resource

A NetBeans Platform Explorer View is a Swing component able to display Nodes, which are generic models (so goodbye forever to DefaultTreeTableModel and its complicated cousins). Several explorer views are provided by the "org.openide.explorer" package. Let's use one of these views to create our own explorer view. The view on the left below is a new Explorer View based on the "org.openide.explorer.view.ListView" class.The starting point for this class comes from nTed (NetBeans Torrent Episode Downloader, which is a work in progress), by Ralph Benjamin Ruijs from Amsterdam.

In this article, you will learn how to create the view below. The "ListView" you see below is an extension of the "org.openide.explorer.view.ListView" class, with its own "CellRenderer". It contains a list of Nodes built from the File menu in the application, rendered as JPanels. When you double-click a Node, the related Action is performed. The selected JPanel is shown in yellow, the other JPanels are cyan.

Let's get started.

  1. Create a JPanel named "ListViewPanel". This is the JPanel you will use to render the Nodes. Create a JLabel named "actionNameLabel" in the ListViewPanel. We're going to create a new JPanel for each Action we find in the "Menu | File" folder in the NetBeans central registry (a.k.a. System FileSystem). Therefore, initialize the JPanel like this:
    public class ListViewPanel extends javax.swing.JPanel {
        public ListViewPanel(Action action) {
            actionNameLabel.setText((String) action.getValue(Action.NAME));

    As you can see, we receive an Action and then use its name to set the JLabel in the JPanel.

  2. Now we'll create our new explorer view (based on "ListView") which will create the JPanel above for each Node in the node hierarchy:
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.Action;
    import javax.swing.JList;
    import javax.swing.ListCellRenderer;
    import org.openide.explorer.view.ListView;
    import org.openide.explorer.view.Visualizer;
    import org.openide.nodes.Node;
    public class ListViewWithCustomCellRenderer extends ListView {
        private JList jList;
        protected JList createList() {
            jList = super.createList();
            jList.setCellRenderer(new ListCellRendererImpl());
            return jList;
        public void setBackground(Color color) {
            if (jList != null) {
        public void setForeground(Color color) {
            if (jList != null) {
        public void setOpaque(boolean opaque) {
            if (jList != null) {
        private static class ListCellRendererImpl implements ListCellRenderer {
            public Component getListCellRendererComponent(JList list, Object value, int row, boolean isSelected, boolean hasFocus) {
                //Convert the model object 
                //(i.e., the content in the explorer view) to a Node:
                Node node = Visualizer.findNode(value);
                //Get our Action from the Lookup of the Node,
                //later you will see how the Action gets there:
                Action action = node.getLookup().lookup(Action.class);
                //Create a new ListViewPanel, passing in the Action:
                ListViewPanel cell = new ListViewPanel(action);
                //Set the background color and the selected color:
                if (isSelected) {
                return cell;
  3. The "ListView" above is pretty simple. In fact, at this point, this article is more or less finished since the focus is about how to create an explorer view and now you know how to do so. However, the crucial point is that the "ListView" above somehow receives an Action class. Here's how, via the "Childfactory" class:
    class FileMenuChildFactory extends ChildFactory<Action> {
        private final FileObject[] fileMenuItems;
        public FileMenuChildFactory(FileObject[] fileMenuItems) {
            this.fileMenuItems = fileMenuItems;
        protected boolean createKeys(List<Action> list) {
            List<FileObject> orderedFiles = FileUtil.getOrder(Arrays.asList(fileMenuItems), true);
            for (FileObject object : orderedFiles) {
                try {
                    DataObject dob = DataObject.find(object);
                    Object instanceObj;
                    //Get the instance of the current DataObject:
                    InstanceCookie ck = (InstanceCookie) dob.getCookie(InstanceCookie.class);
                    try {
                        instanceObj = ck.instanceCreate();
                        //Remove the separators:
                        if (instanceObj instanceof javax.swing.Action) {
                            Action action = (Action) instanceObj;
                    } catch (Exception ex) {
                        instanceObj = null;
                    if (instanceObj == null) {
                } catch (DataObjectNotFoundException ex) {
            return true;
        protected Node createNodeForKey(Action key) {
            return new FileMenuNode(key);
    public class FileMenuNode extends AbstractNode {
        private final Action key;
        public FileMenuNode(Action key) {
            //This is how the Action is made available to the Node,
            //i.e., by being placed into the Lookup of the Node: 
            super(Children.LEAF, Lookups.singleton(key));
            this.key = key;
            setDisplayName((String) key.getValue(Action.NAME));
        public Action getPreferredAction() {
            return new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
        public Action[] getActions(boolean context) {
            return new Action[]{};
  4. Now, as always, you need to implement "ExplorerManager.Provider" in your "TopComponent" and have statements such as these in the constructor of the "TopComponent":
    //Create the node hierarchy:
    FileObject fileMenu = FileUtil.getConfigFile("Menu/File");
    FileObject[] fileMenuItems = fileMenu.getChildren();
    Node rootNode = new AbstractNode(
            Children.create(new FileMenuChildFactory(fileMenuItems), false));
    //Add the ListView to the TopComponent
    setLayout(new BorderLayout());
    ListViewWithCustomCellRenderer elv = new ListViewWithCustomCellRenderer();
    add(elv, BorderLayout.CENTER);

That's all, run the application and you'll have the view shown at the start of this article.



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}