JDK-4789491 : JFileChooser, how to make it select directories?
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_2.5
  • CPU: x86
  • Submitted: 2002-12-05
  • Updated: 2017-05-16
  • Resolved: 2003-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Related Reports
Relates :  
Description
I'm sure that JFileChooser is quite capable of choosing directories.
However, the documentation is sufficiently obtuse as to make it hard
to figure out, and the things I'm trying just don't cut the mustard.

  JFileChooser dir_chooser = new JFileChooser(".");
  dir_chooser.setDialogType(JFileChooser.DIRECTORIES_ONLY);
  FileFilter ff = new FileFilter() {
	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}
		return false;
	}
	public String getDescription() {
		return "Directories";
	}
  };
  dir_chooser.setFileFilter(ff);
  dir_chooser.addChoosableFileFilter(ff);

The resulting dialog only shows directories .. however when I double-click
on the dialog, or if I single-click and click the OPEN button, all that 
happens is the directory is opened.

It's a highly normal thing to desire to open a directory.

Here's what I've observed on my way to deciding to file this:

1- In the javadoc the sections covering the ChoosableFileFilter list
are horribly inadequately described.  Nothing, nada, zip, is there in
the way of describing what the ChoosableFileFilter list is.  If just
says circularly referential statements like "Adds a filter to the
list of choosable file filters", without further discussing what
this means.

2- I finally found the trick:

     dir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

However, the claim still is that the documentation is obtuse.

3- The Swing tutorial referenced by the JFileChooser javadoc does not
cover this highly common scenario.

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b19
14-06-2004

PUBLIC COMMENTS .
10-06-2004

EVALUATION This should be fixed for tiger and we should add it to the tutorial. ###@###.### 2003-01-22 Kathy added it to the tutorial. In the doc, I added a reference from addChooseableFileFilter to setFileSelectionMode. Also fixed erroneous message about exception thrown in setFileSelectionMode (and error message thrown). Approved by CCC. Fixed for tiger. ###@###.### 2003-08-11
11-08-2003