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

Displaying directories only using FileDialog

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all..
I would like to create a dialog box that would allow the user to choose a directory, not a file. Is there any way to use the FileDialog box available in AWT to do this (and using Swing is not an option)?? Or do I need to custom write it myself - if so, any ideas or samples to how to do this would be greatly appreciated!

------------------
Hima
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a class that implements the java.io.FilenameFilter interface and override the accepts method. Use the string that is passed in to create a File object and return the result of its isDirectory() method. Then when you create your file dialog, use the setFilenameFilter method and pass it your custom object.

David
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, FileDialog can't actually use FileName filters on Windows machines, so they are of rather limited use. (You can user the method setFileNameFilter(), but the filter installed never actually gets used.) From the API:

Filename filters do not function in Sun's reference implementation for Windows 95, 98, or NT 4.0.

Basically Sun has decided they will never be able to provide this functionality on those platforms using the original API for FileDialog, and have instead put made JFileChooser the preferred component for this sort of thing. Unfortunately, if you can't use Swing, you can't use JFileChooser, and I don't know a good solution for this problem.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
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!