Swing has been around a long time. I started using it in 1999. Yeah, that's right, an entirely different millenium.
So you'd think there wouldn't be any bugs left...but i think I found one.
Try this:
JFileChooser jfc = new JFileChooser(); // home folder ?
int result = jfc.showSaveDialog((JButton)ev.getSource());
if (result == JFileChooser.APPROVE_OPTION == false) return;
File otherKey = jfc.getSelectedFile();
jfc.setCurrentDirectory(null); // null means homedir
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Now, double-click on your "downloads" folder. Click OK without selecting anything. You would expect to get this:
/Users/<YOURNAME>/Downloads/
but instead you get:
/Users/<YOURNAME>/Downloads/Downloads/
File folderToSaveInto = jfc.getSelectedFile();
//this should resolve things.
if (folderToSaveInto.getName().equals(folderToSaveInto.getParentFile().getName()))
folderToSaveInto = folderToSaveInto.getParentFile();
JFileChooser jfc = new JFileChooser("/Volumes" ); // home folder ?
jfc.setCurrentDirectory(null); // null means homedir
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File folderToSaveInto = jfc.getSelectedFile();
//this should resolve things.
if (folderToSaveInto.getParentFile() == null) {
//leave it alone, it's a root
} else
//well, it didn't, not completely, because of how Windows (feh!) handles drives, top-level folders, and sub-folders.
if (folderToSaveInto.getName().equals(folderToSaveInto.getParentFile().getName()))
folderToSaveInto = folderToSaveInto.getParentFile();