Search code examples
swingjfilechooserfavorites

Swing: JFileChooser with favorites?


I'm working on an application which needs to select files. JFileChooser is a great start, but I need to augment it. I can start it at one particular directory with JFileChooser.setCurrentDirectory(), but how can I offer the user a "favorites" functionality, either in terms of favorite files saved for later, or favorite directories to jump to?


Solution

  • While the JFileChooser accessory was intended to be used for things like thumbnails of the currently selected file, per the JavaDocs:

    An accessory is often used to show a preview image of the selected file; however, it can be used for anything that the programmer wishes, such as extra custom file chooser controls.

    So you'll have to create a custom JComponent to represent "favorite directories", then call setAccessory(myFavesComponent) on your file chooser.

    To actually implement the storage of favorite directories, you'll want to use the Preferences API.