In a Java program, I want to display a JFileChooser where the user selects just a directory name where multiple output files will be written. So, mychooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
; that's easy enough.
However, I'd like to fill in a default directory name. And JFileChooser doesn't appear to allow me to do that. I wrote this:
File newdir = new File(newdirname);
JFileChooser chooser = new JFileChooser(newdir);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int reply = chooser.showSaveDialog(Captions.home);
I was expecting that it would display the parent directory for newdir
with the full path to newdir
filled in. But instead, if newdir
does not already exist, it shows the My Documents directory.
I don't want to create newdir
before calling the JFileChooser, because the user might not take the default. I suppose I could create it and then if the user chooses something else delete it, but that seems rather goofy.
Is there a clean way to do this?
I am not asking how to set a default file name. I am asking how to set a default directory name. This question was closed as a duplicate of one asking about setting a default file name, but the method for setting a default file name doesn't work for setting a default directory name. Maybe there's some additional trick to make it work with a directory name. If so, that's what I'm asking.