Get the path of a directory using JFileChooser

Harsha picture Harsha · Dec 9, 2011 · Viewed 33.9k times · Source

How can I get the absolute path of a directory using JFileChooser, just selecting the directory?

Answer

Wojtek Owczarczyk picture Wojtek Owczarczyk · Dec 9, 2011

Use:

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//or
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

together with:

chooser.getCurrentDirectory()
//or
chooser.getSelectedFile();

then call getAbsoluteFile() on the File object returned.