Thursday Jun 20, 2013

How to Extend the Open File Dialog

You've included the "User Utilities" module (by right-clicking your NetBeans Platform application, choosing Properties, clicking Libraries, and checking the box next to "User Utilities" in the "ide" cluster) and so now you magically have "File | Open File" in the menu bar. When you invoke the Action that has magically been provided, you see the Open dialog, as shown below:

Look at the "Files of Type" filter above. And now look at it again, after I've added the "Image" module to the application (also in the "ide" cluster, as described for the "User Utilities" module above):

The "Image" module adds a new "Files of Type" filter. Now let's say you're creating your own file types, as I did a few days ago for working with ZIP files. I tweaked the @MIMEResolver annotation slightly, which is automatically available at the top of a DataObject created via the New File Type dialog:

        displayName = "#LBL_ZIP_LOADER",
        mimeType = "application/x-zip",
        showInFileChooser = {"ZIP Files", "ZIP archives"},
        extension = {"zip", "ZIP"})

And now I see this, i.e., all thanks to the "showInFileChooser" annotation attribute above:

In other words, use "showInFileChooser" if you want a new filter added to the Open File dialog, to enable the user to filter for files conforming to your custom file type. The value is a String Array, which defines the text to be displayed to the left of the file extension, while the file extension is shown automatically when the "showInFileChooser" attribute is defined. Multiple Strings in the Array is useful, maybe as in the above, in cases where the user might have different understandings of what a ZIP is, i.e., a file or an archive. Fine, provide texts for both understandings. An empty String simply means that nothing will appear to the left of "[.zip]".


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.


« June 2013 »