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.
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.
No.By noel.a on August 19, 2009, 6:08 pmJFileChooser is fine. Your system or your code is the problem. class ChooseFileAction extends AbstractAction { public ChooseFileAction() { super(); ...
Reply | Read entire comment
JFileChooser is seriously brokenBy Anonymous on August 14, 2009, 9:28 pmUser beware. At least in XP, and at least w/ jdk6, the JFileChooser is not a viable option. It take several *minutes* to launch an application that contains nothing...
Reply | Read entire comment
View all comments