Meik Vtune
Meik Vtune

Reputation: 490

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.

Upvotes: 0

Views: 78

Answers (2)

n247s
n247s

Reputation: 1918

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));
}

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

getCurrentDirectory() returns a directory name, not a file name. If the user selects a file, this method returns the name of the directory which contains that file. If you want the name of the file which was selected, you should use getSelectedFile(). If you haven't yet, you should read this Oracle tutorial on file choosers.

Upvotes: 1

Related Questions