In my application, I want the user to be able to select a directory to store stuff in. I have a text field that I'm using to display the directory they've chosen. If they just click on a directory (don't browse it), everything is fine. However, if they double click on the directory and look inside it, the directory name is duplicated.
Ex. They're in the home directory, single click the folder Desktop...path returned is ~/Desktop. On the other hand, if they're in the home directory, double click the folder Desktop, and now are in the Desktop folder, path returned is ~/Desktop/Destkop.
Here's what I'm doing:
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
loadField.setText(f.getPath());
}
I've also tried to do something like chooser.getCurrentDirectory()
but that doesn't really work either.
Edit: Using Mac OS X, Java 1.6