How can I use a JFileChooser to select a directory?

Davanum Srinivas

You can use the DIRECTORIES_ONLY option for the setFileSelectionModel method of JFileChooser. For example:

import javax.swing.JFileChooser;

public class ChooseDir {
   public static void main(String[] args) {
      JFileChooser jfc = new JFileChooser ();
      jfc.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
      System.out.println (jfc.showDialog (null, null));
      System.out.println (jfc.getSelectedFile ());
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.