1

I tried to create a JFileChooser but I don't understand how to set it to show directories only.

1
  • Did you read the API? Did you follow the link to the section in the Swing tutorial on "How to Use File Choosers" where this is discussed? Everybody who answered this question by spoon feeding the answer should be down voted. People need to learn how to use the API and the tutorials before asking basic questions like this. Pointing the OP to the tutorial will give the OP an additional reference to use for solving this question as well as future questions. Commented Jun 9, 2010 at 14:00

3 Answers 3

3
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

if(f.showOpenDialog(parent)== JFileChooser.APPROVE_OPTION) {      
    File result = f.getSelectedFile();
} else {
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

In the tutorial they only guarantee this to work for Java Look & Feel. It may be that with another look & feel files will simply be greyed out.
0

Have look at this code snippet - that sounds promising.

The most intersting line is:

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

Comments

0

Apart from what Michael already suggested you might take a look at JIDE OSS, a free Swing components library, which amongst many other goodies provides a much nicer folder chooser component(FolderChooser).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.