views:

683

answers:

2

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

+1  A: 

Seems to work for me.

import javax.swing.JFileChooser;

public class FChoose {
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = chooser.showOpenDialog(null);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                java.io.File f = chooser.getSelectedFile();
                System.err.println(f.getPath());
            }
        }});
    }
}

6u13 on Vista. Is there something strange about your setup or what you are doing?

If there's a specific bug in a Mac OS X implementation of Java, you may want to, say, check if the file exists and if not de-dupe the last to elements of the path.

Tom Hawtin - tackline
same ver on XP, same positive result.
akf
Edited above, but I'm using Mac OS X and Java 1.6.
knt
And you get the same problem with my complete example?
Tom Hawtin - tackline
Ok actually I feel like an idiot now...used your example, works fine now. I think I was actually using the wrong Dialog type in my own code, but copied it incorrectly when I wrote my question. Sorry for wasting your time and thanks for helping.
knt
Oh, everyone make mistakes! I am interested in what you did wrong in the code you actually used.
Tom Hawtin - tackline
A: 

The problem occurs when you use chooser.showDialog or chooser.showSaveDialog instead of chooser.showOpenDialog. On XP chooser.showDialog returns the correct path under the example given by the OP, but on Mac OS 10.5.7 (and probably earlier versions as well) you'll get ~/Desktop/Desktop . (In my case I need to use showSaveDialog because I want users to have the option to create a new folder, so it looks like I'll have to de-dupe the path manually. It sure looks like this is a bug in the Apple Java implementation.)