Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 93: Add a file finder accessory to JFileChooser

Enhance JFileChooser by implementing your own accessories

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
This tip describes how to extend the functionality of one of the most common user interface components -- the standard file open dialog -- with a threaded file search accessory.

When you attempt to open a file but can't locate it immediately, simply enter your search criteria into the accessory's search fields, hit the Start button, and wait for a list of found files to appear. That search accessory is integrated into the open file dialog and the file search is threaded so you can continue to browse the file system while the search is running.

Adding functionality to Swing's standard file dialog is easy once you understand how to integrate a component into JFileChooser's dialog box, how to make the component responsive to JFileChooser events, and how to control the JFileChooser's file display and selections. I will provide an example accessory with this article. The complete source code for the FindAccessory class is included in Resources. Refer to Jon Sharpe's Java Tip 85 for a review of JFileChooser basics.

Accessorizing JFileChooser



Customizing JFileChooser is easy. Rather than reinvent the standard file dialog to include special functionality, you can implement your custom functionality as a JComponent and integrate it into JFileChooser with a single method call.

    JFileChooser chooser = new JFileChooser();
    chooser.setAccessory(new FindAccessory());


These two lines of code are deceptively simple. On the surface, a FindAccessory component is attached to a standard file open dialog, as illustrated in Figure 1. At a deeper level, FindAccessory is modifying the behavior of JFileChooser. The details of integration are hidden inside the accessory's implementation.

Figure 1. FindAccessory component attached to JFileChooser as it appears with Swing's Metal look and feel



To fully appreciate the power of accessories and the flexibility of JFileChooser, you'll need to understand JFileChooser's properties, events, and control methods. But first, you should know how an accessory component is displayed within the JFileChooser dialog.

Controlling accessory layout

It is especially important to understand how specific layout managers work when implementing complex JFileChooser accessories. Some layout managers, like GridLayout, disregard a component's preferred size. In Java 1.2.2, JFileChooser is too eager to shrink its scrolling list of files to accommodate an accessory. Without some dimensional limitations, a complex accessory can expand to crowd out JFileChooser's file display list and control buttons.

To make layout matters even worse, some components such as text fields and lists, tend to expand to accommodate the width of their content. The rules for sizing JTextFields are particularly complex. Java Swing by Robert Eckstein, Marc Loy, and Dave Wood provides a thorough explanation of text field sizing (see Resources).

In early trials with GridLayout manager, FindAccessory's width would expand during a search to accommodate the widest item in its results list. That expansion often scrunched JFileChooser's file display list to a ridiculously narrow width.

  • 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