Open File Dialog in the NetBeans Platform

When you use the "NetBeans Platform Application" template in NetBeans IDE and you make no changes of any kind, simply run it, you'll find that your "File" menu has this content, by default:

Let's now imagine we want to add an "Open File" menu item to the "File" menu that you see above. Of course, you could create that menu item and the related GUI and functionality yourself.

Or, you could reuse the functionality from NetBeans IDE, which comes from a module named "User Utilities" (org.netbeans.modules.utilities)  and is found in the "ide" cluster. So, to use this module, right-click the application in the Projects window, choose Properties, click Libraries, expand the "ide" node, and put a check in the checkbox next to "User Utilities". The red "Resolve" button now becomes enabled. Click it and the Search API also becomes selected.

Now run your application again and, without having typed a single character of code, you will see the following:


The "Open File" dialog looks as follows:

Above, notice that two filters are predefined, for text files and for Java files; however, you're not limited to accessing these files, simply use the "All Files" filter and you'll be able to access any file. Depending on which modules you have installed in your application, different things can happen to the files that you open. For example, if you have the "Image" module installed, the image opens into the Image Editor when you use the Open File dialog to open an image file.

Now, let's add our own filter to the Open File dialog! In one of your custom modules, set a dependency on the "User Utilities" module (it does not expose an API, so you need to set a dependency on the implementation and declare the User Utilities module as your friend), as well as the "Lookup API" module.

Then you can create a new Open File dialog filter class, like this:

package org.my.filter;

import org.netbeans.modules.openfile.OpenFileDialogFilter;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class)
public class MyOpenFileDialogFilter extends OpenFileDialogFilter {
    @Override
    public String getDescriptionString() {
        return "My Files";
    }
    @Override
    public String[] getSuffixes() {
        return new String[]{".my"};
    }
}

When you run the application, your Open File dialog will now be as follows:

However, what if you only want your own filters to be displayed and not those provided by default by the "User Utilities" module? Here, thanks to this issue, is the solution, note the bit in bold:

@ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class,
supersedes = {
    "org.netbeans.modules.openfile.FileChooser$TxtFileFilter",
    "org.netbeans.modules.openfile.FileChooser$JavaFilesFilter"
})
public class MyOpenFileDialogFilter extends OpenFileDialogFilter {

    @Override
    public String getDescriptionString() {
        return "My Files";
    }

    @Override
    public String[] getSuffixes() {
        return new String[]{".my"};
    }
}

The result:


Another consideration is that maybe you don't want all of the "User Utilities" module. I.e., there's quite some packages in there that do not relate to the Open File dialog. In that case, copy the "org.netbeans.modules.openfile"  package into your own module (optionally, delete the Handler class, which depends on the Command Line Parsing API, which might not be a dependency you'd like to take with you in your application) and then you have forked the Open File dialog functionality from NetBeans IDE into your own application. That will also save you the inconveniences associated with needing to set an implementation dependency on "User Utilities", which is also a win.

Many thanks to Sam Harwell from San Francisco for help and questions on this scenario! Hope the above helps, Sam.

Comments:

How do we enable the "Open Recent File" feature that was also exposed?

Posted by Bill Ooms on September 23, 2011 at 08:06 AM PDT #

Just open a file...

Posted by Geertjan on September 23, 2011 at 09:16 AM PDT #

"set a dependency on the implementation" - OK, but will only work for one release. "and declare the User Utilities module as your friend" - no, this is impossible (and would contradict the first advice even if it worked).

Anyway *.java should likely _not_ be hardcoded in that dialog. I am not sure there is any purpose for the filters at all, since any openable file (pretty much anything) could be selected. Probably the filters should simply be removed altogether.

"there's quite some packages in there that do not relate to the Open File dialog" - probably this module should be split up. There is no particular reason why it should contain all the things that it does; it was just a grab bag at some point in the distant past and no one ever bothered to refactor it.

Posted by Jesse Glick on September 26, 2011 at 06:42 AM PDT #

sir,i want to open .tiff image file using Open file Dialog in netbeans platform App. so how can i do that?

Posted by guest on May 21, 2012 at 02:48 AM PDT #

Let's take this a step further. Let's say that TopComponent contains a Bean component that has it's equivalent of a load() function. Specifically, Batik's JSVGCanvas has a .setURI() function.

How can I override the Netbeans Module 'Open File' functionality if I have absolutely no code to alter? Is this controlled in a property? Do I need to extend and override this module, or is this module approach simply worthless to anyone doing anything but loading a text file to a component in the editor node?

Posted by Rook on August 06, 2012 at 02:25 PM PDT #

Please note that OpenFileDialogFilter will be deprecated in NetBeans 7.3.
See new attribute showInFileChooser in MIMEResolver.Registration:

http://bits.netbeans.org/dev/javadoc/org-openide-filesystems/org/openide/filesystems/MIMEResolver.Registration.html#showInFileChooser%28%29

Posted by jhavlin on October 02, 2012 at 01:04 AM PDT #

Yeah guys, i would want to dictate what happens when i open a.file with a certain extension. I'm using netbeans 7.3 so I'm going to check out your link jhavlin

Posted by Winifred on May 27, 2013 at 10:14 PM PDT #

I'm wondering if it's possible to not show the all files also

Posted by Winnifred on May 28, 2013 at 12:42 AM PDT #

Write to the dev@platform.netbeans.org mailing list, thanks.

Posted by Geertjan on May 28, 2013 at 12:55 AM PDT #

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