Over a million developers have joined DZone.

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

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}