Geertjan's Blog

  • September 23, 2011

Open File Dialog in the NetBeans Platform

Geertjan Wielenga
Product Manager

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 {
    public String getDescriptionString() {
        return "My Files";
    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 = {
public class MyOpenFileDialogFilter extends OpenFileDialogFilter {
    public String getDescriptionString() {
        return "My Files";
    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.

Join the discussion

Comments ( 9 )
  • Bill Ooms Friday, September 23, 2011

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

  • Geertjan Friday, September 23, 2011

    Just open a file...

  • Jesse Glick Monday, September 26, 2011

    "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.

  • guest Monday, May 21, 2012

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

  • Rook Monday, August 6, 2012

    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?

  • jhavlin Tuesday, October 2, 2012

    Please note that OpenFileDialogFilter will be deprecated in NetBeans 7.3.

    See new attribute showInFileChooser in MIMEResolver.Registration:


  • Winifred Tuesday, May 28, 2013

    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

  • Winnifred Tuesday, May 28, 2013

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

  • Geertjan Tuesday, May 28, 2013

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

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