In Swing, the JFileChooser pointed to the user's default directory which is typically the "My Documents" folder in Windows. The JavaFX FileChooser does not have the same behavior by default. There is a setInitialDirectory
method which should be fine, however there are a number of places in the application that we open FileChoosers. Unfortunately the FileChooser class is final, so I cannot simply extend the class and just call the setInitialDirectory
once. Is there anything else I could do besides going through the entire application and adding the setInitialDirectory
calls?
There's the obvious solution, to just create a static utility method somewhere:
public class MyUtilities {
public static FileChooser createFileChooser() {
FileChooser chooser = new FileChooser();
chooser.setInitialDirectory(new File(System.getProperty("user.home"));
return chooser ;
}
}
Then you can just do
FileChooser chooser = MyUtilities.createFileChooser();
whenever you need one.
I actually prefer, from a user experience perspective, to use a single FileChooser
instance for the whole application (or at least for each functional portion of a large application). That way it maintains the last directory the user visited, which is more convenient imho.