Meik Vtune Meik Vtune - 3 months ago 18
Java Question

Understanding JFileChooser behaviour

I am trying to open a

JFileChooser
dialog to let the user decide his wish-directory for following operations.

Following is my current code:

JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
chooser = new JFileChooser(pref.get("LAST_PATH", ""));
} else{
chooser = new JFileChooser(home_dir);
}
//chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
int retVal = chooser.showOpenDialog(frame);
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory().toString());


home_dir is a static String pointing to the users Download-directory.

The behaviour I do not understand:

home_dir = C:/Users/Scy/Downloads


Press OK without selecting any file(Or directory)

Output: C:/Users/Scy





home_dir = C:/Users/Scy/Downloads


Select any file within Downloads

Output: C:/Users/Scy/Downloads


Why do I not get the full-path(
C:/Users/Scy/Downloads
) as output when not selecting anything and just pressing OK? (With
DIRECTORIES_ONLY
activated, can't press OK without selecting anything without
DIRECTORIES_ONLY
)

Edit: I just noticed that when I just press the Cancel button without selecting anything the output is indeed what I expect,
C:/Users/Scy/Downloads
.

Based on an answer on this post I tried the following:

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(home_dir)); //home_dir = "C:/Users/Scy/Downloads"


The result was exactly the same as above. Pressing the Cancel button results in the full path output, while pressing OK/Accept results in
C:/Users/Scy
.

Answer

Maybe the 'selected file'(or directory) is in the 'current directory' (which you are retrieving atm.)?

If you want the current selected file, chooser.getSelectedFile() is what you are looking for. Keep in mind that when switching to DirectoryOnly mode this method will return a directory (e.g. a File instance representing a directory).

The method chooser.getCurrentDirectory() will return the parent directory of the current selected file which explains the unexpected results. (getSelectedFile.getParentFile() will most likely return the same file)


If you are trying to retrieve the parentDirectory, you set the starting directory incorrect. Notice how you pass in the first constructor a selected File? This means in the second constructor the 'home_dir' will be the selected File. If you only want to set 'home_dir' as starting directory, you should use the no-args constructor and call chooser.setCurrentDirector(new File(home_dir)) instead. Here is a snippit of what your code could look like:

JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
    // set last SELECTED file/directory path.
    chooser = new JFileChooser(pref.get("LAST_PATH", ""));
 } else{
     // set currentDirectory,  but dont select anything yet.
     chooser = new JFileChooser();
     chooser.setCurrentDirectory(new File(home_dir));
}