Rahul K Jha
Rahul K Jha

Reputation: 788

How to Get selected folder name in java using JFileChooser?

I want to select the folder that is selected.

 JFileChooser targetDir = new JFileChooser();
            targetDir.setDialogTitle("Choose Target Directory.");
            targetDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if(targetDir.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)   
            {
                System.out.println(targetDir.getCurrentDirectory());
                main_mw = new MainWindow("XYZ Copier");
            main_mw.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                        } else {
                System.exit(0);
            }
        } else {
        }

It gives the output "/home/rahul/Downloads/mc" but I need "/home/rahul/Downloads/mc/lib". It gives same result if i go inside lib.

Screenshots:

Upvotes: 0

Views: 1345

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347234

JFileChooser#getSelectedFile will return the selected file/directory

getCurrentDirctory returns the directory which is currently been shown in the chooser

Upvotes: 2

Related Questions