Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Quickly access files and directories you use repeatedly

The JFileChooser shortcuts accessory organizes your documents

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Developers have suggested many approaches for accelerating the selection of a directory or file in JFileChooser. TypeAheadSelector allows you to quickly select a specific file in the current directory by typing just the initial letters of its name (suitable for JFileChooser in J2SE (Java 2 Platform, Standard Edition) 1.3 and earlier versions—the chooser in J2SE 1.4 sports a built-in type-ahead feature). A history mechanism maintains a list of recently visited directories, allowing you to easily revisit the same directory. Some applications allow you to create and maintain a list of files that you can access from a menu. For instance, Microsoft Word XP allows you to add a Work menu to the Main menu and then add currently opened documents to the Work menu for quick access in the future.

In this article, I describe a JFileChooser shortcuts accessory that goes further than the Work menu. The accessory allows both files and directories in the list. In addition, you can define a short name (alias) for a given shortcut that proves easier to read than an absolute path. You can further improve your shortcuts' readability by coding them in a specific color. Finally, the accessory also updates the chooser's dialog title to show the path to the currently selected directory, which helps you navigate the file system.

Note: You can download this article's source code from Resources.

The shortcuts accessory

The accessory consists of a list of shortcuts and, below that, a control panel, as shown in the figure below.

Shortcuts accessory

The list's shortcuts are sorted alphabetically. A shortcut displays as either an absolute path to a directory/file or, for shortcuts with defined aliases, as its alias enclosed in brackets. For instance, four aliases are defined in the figure above: Flights Retriever, Java Source, Tomcat-Apps, and Tomcat-Work. When the mouse moves over an alias, a tool-tip text displays for that alias, showing the path to a file or directory represented by that alias.

To change the chooser's current directory to the one represented by the shortcut, you click on a shortcut in the list. If the selected shortcut represents a file, the accessory selects that file in the chooser, and the file's name appears in the File Name field. The accessory modifies the chooser's dialog title to include the chooser's current directory path in parentheses.

You can easily add/remove the list's shortcuts and set/reset your aliases. Clicking the Add button adds the chooser's current directory to the list or, if the accessory selects a file in the chooser, the selected file is added to the shortcuts list. The accessory ensures no duplicates appear in the list. Clicking the Delete button removes the currently selected shortcut from the list. To set a given shortcut's alias, first select this shortcut, then type any text in the Alias field, and either press Enter on your keyboard or click the Set button. To change a shortcut's color, add a color name and hash character (#) to an alias (for example, blue#Tomcat). When a shortcut with a nonempty alias is selected in the list, the alias also displays in the Alias field so you can easily rename or remove it (again, press Enter or click Set for confirmation). If you delete the shortcut's alias in the Alias field, this shortcut again displays as its absolute path.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources