NetBeans APIs Outside of the NetBeans Platform

Jarda and I are in Budapest visiting a team of developers. Somewhere along the line, I think in the taxi on the way from the hotel to the site, he knocked together an application that uses the NetBeans APIs, without using the NetBeans Platform. In other words, there's no module here. Just a plain Java application. The end result is a file browser that looks as follows:

And this is the source structure of the application. Of course, take special note of the NetBeans API JAR files. You can easily get them from your distribution of NetBeans IDE and then simply add them to your own application:

By the way, Jarda added the beans from the org.openide.explorer.ExplorerManager JAR to the Palette, in order to be able to drag and drop the two views on the JFrame! He says that ideally the apisupport project should do that by default. Here's how it looks:

Here's FileNode.java:

import java.io.File;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;

public final class FileNode extends AbstractNode {
    
    private File file;
    
    private FileNode(File f) {
        super(new FileKids(f));
        file = f;
        setName(f.getName());
    }
    
    public static Node files() {
        AbstractNode n = new AbstractNode(new FileKids(null));
        n.setName("Root");
        return n;
    }
    
    private static final class FileKids extends Children.Keys<File> {
        File file;

        public FileKids(File file) {
            this.file = file;
        }
        
        @Override
        protected void addNotify() {
            if (file == null) {
                File[] arr = File.listRoots();
                if (arr.length == 1) {
                    arr = arr[0].listFiles();
                }
                setKeys(arr);
            } else {
                File[] arr = file.listFiles();
                if (arr != null) {
                    setKeys(arr);
                }
            }
        }

        @Override
        protected Node[] createNodes(File f) {
            FileNode n = new FileNode(f);
            return new Node[] { n };
        }
        
    }

}

Here's Explorer.java:

import org.openide.explorer.ExplorerManager;

public class Explorer extends javax.swing.JFrame implements ExplorerManager.Provider {

    private ExplorerManager em;
    
    /\*\* Creates new form Explorer \*/
    public Explorer() {
        em = new ExplorerManager();
        em.setRootContext(FileNode.files());
        initComponents();
    }
    
    /\*\* This method is called from within the constructor to
     \* initialize the form.
     \* WARNING: Do NOT modify this code. The content of this method is
     \* always regenerated by the Form Editor.
     \*/
    //                           
    private void initComponents() {

        contextTreeView1 = new org.openide.explorer.view.ContextTreeView();
        listView1 = new org.openide.explorer.view.ListView();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(contextTreeView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(listView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, listView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, contextTreeView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))
                .add(23, 23, 23))
        );

        pack();
    }//                         
    
    /\*\*
     \* @param args the command line arguments
     \*/
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Explorer().setVisible(true);
            }
        });
    }

    public ExplorerManager getExplorerManager() {
        return em;
    }
    
    // Variables declaration - do not modify                     
    private org.openide.explorer.view.ContextTreeView contextTreeView1;
    private org.openide.explorer.view.ListView listView1;
    // End of variables declaration                   
    
}

Finally, here's Main.java:

public class Main {

    /\*\*
     \* @param args the command line arguments
     \*/
    public static void main(String[] args) {
        Explorer.main(args);
    }

}

Comments:

Well, this is really cool. Probably we shouldn't be suprised, but so far the only NetBeans APIs I've talked about a plain J2SE use was the Lookup and the Visual Library. I'm going to add this for my next talk about the platform.

Posted by Fabrizio Giudici on October 05, 2007 at 01:42 AM PDT #

This is very interesting -- and timely, in light of the fact that I was discussing exactly this with some developers last week.

What I wonder is, how much of the API can you use in a regular Swing application (i.e. not as modules)? While the code is pure Java, I think that there has got to be at least some places that are dependent on being loaded as module (classloader issues) or that depend on core infrastructure (for example, maybe the Dialogs API assumes that you're running within the WindowManager class instead of simply a JFrame.

Posted by Tom Wheeler on October 05, 2007 at 03:13 AM PDT #

Hi Fabrizio and Tom. Cool to see interest in this. I'm sure thst this approach isn't possible for all NetBeans APIs, but for those where it does work, it is really cool.

Posted by Geertjan on October 06, 2007 at 07:39 PM PDT #

Geertjan- this is a perfect way for desktop java developers to migrate from using pure Swing to using the Netbeans platform in small incremental steps. Plunging into desktop app development based on the Netbeans platform is still a bit daunting for mere mortals even with excellent resources like your blog and your book. Thanks.

Posted by Bob Namestka on October 26, 2007 at 04:04 AM PDT #

I could not agree with you more, Bob!

Posted by Geertjan on October 26, 2007 at 04:06 AM PDT #

Could someone give me the project ready? please
I have problems with the code,I have no idea why.

Posted by George L. on February 23, 2011 at 08:53 AM PST #

Can you give even a small clue about what's going wrong? Otherwise you will not get any help, not from me anyway.

Posted by Geertjan Wielenga on February 24, 2011 at 06:11 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

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.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today