This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Select directory with JFileChooser

 
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big Thanks!
 
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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
 
If you're gonna buy things, buy this thing and I get a fat kickback:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!