• Post Reply Bookmark Topic Watch Topic
  • New Topic

Select directory with JFileChooser

 
Steffen Foldager
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I want to select only a directory through a dialog-like frame.
It should be possible to highlight a folder, and then press OK.
FileDialog in java.awt doesn't let me do that becuase you always have to select a file.
JFileChooser is closer, but I have problems using the follwing code snippet:
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Select target directory");
int returnVal = chooser.showOpenDialog(parentFrame);
if(returnVal == JFileChooser.APPROVE_OPTION) {
myFile = chooser.getSelectedFile();
System.out.println(beanFile.getAbsolutePath() + beanFile.isDirectory());
}
I does allow me to select a directory, but the absolute path for e.g. the directory C:\MyDir is printed as C:\MyDir\MyDir
Further, the isDirectory() method always return false no matter what is selected.
I'm using jdk1.3 and the file system is NTFS on a WinNT 4.0 SP 5.
Am I doing something wrong?
Regards,
Steffen
 
Steffen Foldager
Ranch Hand
Posts: 58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear dairy:
Got it now. Just use
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Steffen
 
wirawit chaochaisit
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big Thanks!
 
Adam Bannach
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffen Foldager wrote:Dear dairy:
Got it now. Just use
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Steffen


I really appreciate the answer here, and the humor was perfect. I know this was 12 years ago, but I don't care. Hope you've been fruitful in all your endeavors.
 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!