United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4789491 JFileChooser, how to make it select directories?
JDK-4789491 : JFileChooser, how to make it select directories?

Details
Type:
Bug
Submit Date:
2002-12-05
Status:
Resolved
Updated Date:
2003-09-05
Project Name:
JDK
Resolved Date:
2003-09-05
Component:
client-libs
OS:
solaris_2.5
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.4.1
Fixed Versions:
5.0 (tiger)

Related Reports
Relates:

Sub Tasks

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
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
                                     
2003-08-11
PUBLIC COMMENTS

.
                                     
2004-06-10
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger

FIXED IN:
tiger

INTEGRATED IN:
tiger
tiger-b19


                                     
2004-06-14



Hardware and Software, Engineered to Work Together