I tried to create a JFileChooser but I don't understand how to set it to show directories only.
-
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.camickr– camickr2010-06-09 14:00:10 +00:00Commented Jun 9, 2010 at 14:00
Add a comment
|
3 Answers
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(f.showOpenDialog(parent)== JFileChooser.APPROVE_OPTION) {
File result = f.getSelectedFile();
} else {
...
}
1 Comment
Max Leske
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.
Have look at this code snippet - that sounds promising.
The most intersting line is:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Comments
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).