Can JFileChooser display files, but only allow directory selection?
posted 11 years ago
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?
Dave Taubler<br />Specializing in <a href="http://taubler.com/articles/" target="_blank" rel="nofollow">Java and Web Development</a>
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.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads