Newsletter sign-up
View all newsletters

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

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Speed up file searching in JFileChooser

Implement a type-ahead feature for faster file selection

  • Print
  • Feedback
You'd be hard-pressed to find an application these days that does not require a user to choose a file at some stage. To cater to that need, the Swing collection provides a JFileChooser component that makes it easy to visually navigate the filesystem and select a file or directory from a list.

Traditionally, users select a file in the file chooser's dialog by scrolling the chooser's list with a mouse and double-clicking the desired file. That method is fine for directories with a small number of files, but it becomes cumbersome and time-consuming when you're dealing with large directories with hundreds of files.

This article demonstrates how to implement an alternative way of choosing files by typing the first few characters of a filename. Once the selection bar is on the desired name, the user presses the Enter key to choose it. The type-ahead feature is implemented for files only, but you can easily extend it to include subdirectories as well.

Listening to user keystrokes

The JFileChooser is a compound component, consisting of a number of child components. Those child components are standard Swing components, such as JButton, JPanel, JComboBox, JList, and others. JFileChooser has only one JList component, which is wrapped in a scroll pane, and used to display subdirectories and files in the current directory (as shown in Figure 1). I've highlighted that component with a red border.

Figure 1. JList descendent component in JFileChooser



To implement the type-ahead feature in JFileChooser, the utility must know the user's keystrokes. You could do that by registering the utility as a key listener on the list. Unfortunately, the list is declared private, so the utility does not have direct access to it. You can, however, obtain a reference to the list: the getComponents() method of the Container class returns all components added to a specified container, including those declared private.

Now you can easily go through a list of all JFileChooser's descendant components to find the only JList component. A recursive method seems the most natural in this situation:

private Component findJList(Component comp) {
    if (comp.getClass() == JList.class) return comp;
    if (comp instanceof Container) {
        Component[] components = ((Container)comp).getComponents();
        for(int i = 0; i < components.length; i++) {
            Component child = findJList(components[i]);
            if (child != null) return child;
        }
    }
    return null;
}


Initially the findJList() method is passed a JFileChooser object, which itself is a container. The method retrieves all child components of that container by means of the getComponents() method, and recursively calls itself until a descendent component is found, which is an instance of JList. If the method is passed a component that is neither an instance of JList nor a container, it returns a null value to indicate that recursion process should continue. Once a JList component is found, findJList() exits from the for loop returning that component, which ends recursion calls.

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources