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. But 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 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?

Note that I am NOT asking how to set a default FILE name. I am asking how to set a default DIRECTORY name. Someone closed this question 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.

Jay
  • 25,388
  • 9
  • 54
  • 105

0 Answers0