Geertjan is a DZone Zone Leader and has posted 459 posts at DZone. You can read more from them at their website. View Full User Profile

Darker NetBeans API Corners: org.openide.filesystems.FileChooserBuilder

08.12.2010
| 3932 views |
  • submit to reddit

Now that the new NetBeans Platform book, NetBeans Platform 6.9 Developer's Guide (Packt, August 2010), has been published, the main APIs of the NetBeans Platform have been dealt with in numerous different ways, via tutorials, reference guides, and now the new step-by-step procedural book. The time has come to step back from those main APIs and look at what else the NetBeans Platform offers. In an occasional series, starting with this one, I will highlight as many of the lesser known yet useful NetBeans APIs, i.e., mostly the utility-oriented and ancillary features. At the end of the series, a pile of APIs should have been covered, which can then be fed into a new book, of course. So, consider this the start of an appendix to the next book, with the appendix focusing on aspects of the NetBeans Platform that are worth knowing about in specific scenarios. Please feel free to nominate NetBeans APIs to be dealt with in this series!

FileChooserBuilder is a utility class for working with JFileChoosers. In particular, remembering the last-used directory for a given file is made transparent. You pass an ad-hoc string key to the constructor (the fully qualified name of the calling class is good for uniqueness, and there is a constructor that takes a Class object as an argument for this purpose). That key is used to look up the most recently-used directory from any previous invocations with the same key. This makes it easy to have your user interface “remember” where the user keeps particular types of files, and saves the user from having to navigate through the same set of directories every time they need to locate a file from a particular place.

FileChooserBuilder's methods each return "this", so it is possible to chain invocations to simplify setting up a file chooser. Example usage:

//The default dir to use if no value is stored
File home = new File (System.getProperty("user.home") + File.separator + "lib");
//Now build a file chooser and invoke the dialog in one line of code
//"libraries-dir" is our unique key
File toAdd = new FileChooserBuilder ("libraries-dir").setTitle("Add Library").
setDefaultWorkingDirectory(home).setApproveText("Add").showOpenDialog();
//Result will be null if the user clicked cancel or closed the dialog w/o OK
if (toAdd != null) {
//do something
}

Instances of this class are intended to be thrown away after use. Typically you create a builder, set it to create file choosers as you wish, then use it to show a dialog or create a file chooser you then do something with.

Supports the most common subset of JFileChooser functionality; if you need to do something exotic with a file chooser, you are probably better off creating your own.

Note: If you use the constructor that takes a Class object, please use new FileChooserBuilder(MyClass.class), not new FileChooserBuilder(getClass()). This avoids unexpected behavior in the case of subclassing. 

Nested classes:

  • FileChooserBuilder.BadgeProvider. Provides "badges" for icons that indicate files or folders of particular interest to the user.

  • FileChooserBuilder.SelectionApprover. Object which can approve the selection (enabling the OK button or equivalent) in a JFileChooser. Equivalent to overriding JFileChooser.approveSelection().

The above text comes directly from the NetBeans API javadoc. Further reading: http://bits.netbeans.org/dev/javadoc/org-openide-filesystems/org/openide/filesystems/FileChooserBuilder.html
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Tim Boudreau replied on Thu, 2010/09/02 - 12:57pm

Happy to see it get some publicity - I added it to the API fairly quietly a year or two ago. Someone still needs to mow through NetBeans code and modify everything to use it - we could delete thousands of lines of code in the process. -Tim

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.