0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay
  • 26,876
  • 10
  • 61
  • 112
  • 1
    This question is being [discussed on Meta](https://meta.stackoverflow.com/questions/411344/what-do-i-do-if-one-of-my-questions-is-closed-for-reasons-i-consider-misguided). – TylerH Sep 08 '21 at 14:55
  • @user16320675 setCurrentDirectory sets the parent directory from which the user can choose a file or a directory. But if the goal is for the user to choose a directory, this doesn't help. You're setting the parent directory in which the user can choose a subdirectory. You're not setting a default subdirectory for the user to choose. – Jay Sep 08 '21 at 21:19
  • @user16320675 Interesting idea. Seems like a lot of work to get there, but maybe that is the "right" way to do it. I'll have to tinker with that. – Jay Sep 09 '21 at 16:55

1 Answers1

0

I ultimately "solved" this with an ugly and kludgy workaround. Until I hear something better, here's what I did:

I created the directory that I want to be the default before opening the JFileChooser. Then I could select it as the default "file". When I get control back from the JFileChooser, if the selected file name is this default directory, then great, I run with it. Otherwise -- the user cancelled the JFileChooser or the selected file does not match the default -- then I delete the "tentative" default directory that I created.

(Oh, before deleting I make sure that it's empty, so just in case in the time between when you opened the JFileChooser and when you selected some other directory, somebody else jumped in and created a file in that directory, I don't blow it away.)

Creating the directory and then deleting it if it's not selected seems pretty goofy, but it does work, which is better than other things I tried that don't work, so...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay
  • 26,876
  • 10
  • 61
  • 112