Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can JFileChooser display files, but only allow directory selection?  RSS feed

 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22678
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!