choose Dir from JFileChooser - Java Swing

Java examples for Swing:JFileChooser

Description

choose Dir from JFileChooser

Demo Code


//package com.java2s;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    public static File chooseDir(String startPath, String dlgTitle) {
        boolean flgDebug = false;
        JFileChooser chooser = new JFileChooser(startPath);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        File f = null;/*w ww . j  a v a  2  s .  co  m*/
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "Audio Files", "wav", "mp3", "flac");
        //chooser.setFileFilter(filter);
        int returnVal = chooser.showDialog(new JFrame(""), dlgTitle);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            f = chooser.getSelectedFile();
            if (flgDebug)
                System.out.println("You chose to open this file: "
                        + f.getName());
        }
        return f;
    }
}

Related Tutorials