Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFileChooser getCurrentDirectory returning wrong current directory?

I am using JFileChooser in an app to browse for a directory however when I select the directory it returns the path to the folder above the folder I selected. i.e. I select "C:\Test" and it returns "C:\"

Here is the Code I'm Using

            JFileChooser c = new JFileChooser();
            c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int rVal = c.showSaveDialog(c);
            if (rVal == JFileChooser.APPROVE_OPTION) {
                txtDirectory.setText("");
                CC_Test.MsgBox(c.getCurrentDirectory().toString());
                txtDirectory.setText(c.getCurrentDirectory().toString());
            }
            if (rVal == JFileChooser.CANCEL_OPTION) {
                txtDirectory.setText("");
            }
like image 1000
Talon06 Avatar asked Jan 06 '12 19:01

Talon06


1 Answers

You should use

c.getSelectedFile()

instead of

c.getCurrentDirectory()

in order to get the selected file (aka directory in this case). Otherwise it yields the directory which is shown in the filechooser's panel (which is the parent) and not the one which is selected.

like image 149
Howard Avatar answered Sep 23 '22 15:09

Howard