Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:28:19+00:00 2026-06-01T14:28:19+00:00

(Thanks in advance! Please let me know if you need more info. Sample code

  • 0

(Thanks in advance! Please let me know if you need more info. Sample code at the bottom.)

Problem I’m trying to solve:

I’m trying to get this JFileChooser object to display only directories (and not files), through the use of a javax.swing.filechooser.FileFilter object that has this in the accept(File file) overridden method: return file.isDirectory();. However, at least on my mac, it doesn’t seem to prevent files from being displayed along with the directories (it does prevent files from being selected without using the setFileSelectionMode() method).

Question

Am I missing something? If not, has anyone ever encountered this before?

My understanding/assumptions:

  1. The magic should happen when you pass in a javax.swing.filechooser.FileFilter object into the JFileChooser’s setFileFilter() method.
  2. Seems like my JFileChooser with setFileFilter() is behaving like its using of setSelectionMode( JFileChooser.DIRECTORIES_ONLY );

Code

import java.io.File;
import javax.swing.filechooser.FileFilter;

// inside a method that's adding this to a JPanel

_fileChooser = new JFileChooser( "." );
_fileChooser.setControlButtonsAreShown( false );
_fileChooser.setFileFilter( new FolderFilter() );
// _fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
_panelMidLeft.add( _fileChooser );

// an inner class, defined somewhere else in the class

private class FolderFilter extends javax.swing.filechooser.FileFilter {
  @Override
  public boolean accept( File file ) {
    return file.isDirectory();
  }

  @Override
  public String getDescription() {
    return "We only take directories";
  }
}

Thanks!

Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:28:21+00:00Added an answer on June 1, 2026 at 2:28 pm

    Your code works for me. My SSCCE:

    import java.io.File;
    import javax.swing.JFileChooser;
    
    public class ShowDirectoriesOnly {
       public static void main(String[] args) {
          JFileChooser fileChooser = new JFileChooser( "." );
          fileChooser.setControlButtonsAreShown( false );
          fileChooser.setFileFilter( new FolderFilter() );
          fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
          fileChooser.showOpenDialog(null);
       }
    
       private static class FolderFilter extends javax.swing.filechooser.FileFilter {
          @Override
          public boolean accept( File file ) {
            return file.isDirectory();
          }
    
          @Override
          public String getDescription() {
            return "We only take directories";
          }
        }
    }
    

    If you’re still having problems, your best is to create your own SSCCE that demonstrates your problem.

    Edit

    Screenshot on how it looks under OS X with JDK1.7

    OS X screenshot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks in advance. This is my sample code. Whenever i try to run it
Please let me know how to check if any checkbox is checked in a
thanks in advance for your help. I am wondering if there is a (design)
Thanks in advance for your help experts. I want to be able to copy
Thanks in Advance for reading and answer this question. I got button in asp
thanks in advance for reviewing my post. I've only been using jquery for about
Thanks in advance.. I have a windows phone application. In that I am showing
thanks in advance for the replies.... I have started to create a network topology
Thanks in advance. How can I stop downloading when some events occurred in wp7.For
Thanks,in advance I want to remove all the zero size files in specified directory,can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.