• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can JFileChooser display files, but only allow directory selection?

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JFileChooser in which a user needs to select a save-to directory (to which to save a batch of files). After using it, a few people have commented that it would be good to allow users to actually see the files that exist in the directory, before they choose that directory (which makes sense, IMHO). But again, the user shouldn't be allowed to actually select a file.

Problem is, JFileChooser seems to tightly couple what a user can see and what they can select. I've been playing around with a few kludges without anything I results that I consider satisfactory. For example, set the file selection mode to FILES_AND_DIRECTORIES, and if the user selects a file, then behind-the-scenes just use that file's parent. But that's non-intuitive and misleading from a UI perspective. Or, use FILES_AND_DIRECTORIES mode but if the user selects a file, then set the current selection to that file's parent... but that causes the UI to jump around.

So, is there something basic I am missing? Or has anyone ever successfully tackled that problem?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I read the docs, "setFileFilter(FileFilter)" allows you to "filter out files from the user's view", i.e. to control what the user sees, and "setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)" allows the user to "just select directories".

It's possible that the documentation is badly worded here. But if it's correct then you can do what you want. I haven't tried it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic