Clashsoft
Clashsoft

Reputation: 11882

JFileChooser won't allow selection of directories

I want to use a JFileChooser in my program in order to choose a directory and process it. However, no matter what FileFilter I use for the file chooser, the Open button is locked when a directory is selected. Below is the code of my FileFilter.

this.fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
    @Override
    public String getDescription()
    {
        return "Directories";
    }

    @Override
    public boolean accept(File f)
    {
        return f.isDirectory();
    }
 });

Upvotes: 2

Views: 205

Answers (1)

Adam
Adam

Reputation: 36703

Have you tried setting the file selection mode? The default is JFilesChooser.FILES_ONLY, which means your custom FileFilter is effectively ignored even though you return true for directories.

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

or

chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

Upvotes: 6

Related Questions