Select Directory using JFileChooser

Submitted by:Jhon Smith

Date added:25 March, 2013

Category:Java

How to select directory using JFileChooser

Tags: select directory

Code Snippet:

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle( "Select Directory" );
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false); // disable the "All files" option.
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
} else { /* ignore if user presses cancel */ }
 
 

Comments