2

I need to restrict my JFileChooser to a directory and its children, because the file should only be saved on external media. I won't run into any problems with the external memory not being present, that has already been handled.

Should I...

  • Override getCurrentDirectory()?
  • Use a FileFilter? Normally these use String.endsWith(), but I would get the absolute path and restrict based on the path to the external device.
gobernador
  • 5,659
  • 3
  • 32
  • 51
  • The first one probably won't do what you want, and if the second will it will confuse users. (Since most everything will be disabled without a clear explanation why.) I'd just default to external media, and display a warning / error if the user picks a location outside of one. – millimoose Jul 12 '12 at 15:20
  • possible duplicate of [How do I restrict JFileChooser to a directory?](http://stackoverflow.com/questions/32529/how-do-i-restrict-jfilechooser-to-a-directory) – dacwe Jul 12 '12 at 15:27

1 Answers1

0

I would absolutely avoid implementing such restrictions. Its almost impossible to get this right (how can you determine reliably what is external storage?). It goes against user expectations.

A better approach would be to let the user pick any directory he likes, and when you detect the directory is probably unsuitable, display a Warning detailing why it is unsuitable. Offer two choices on the warning, like "Understood, proceed anyway" and "Pick another directory". And possibly an option to turn the warning off somewhere in the program options.

Durandal
  • 19,919
  • 4
  • 36
  • 70