7

(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

Alex Ku
  • 125
  • 2
  • 6
  • Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. – Andrew Thompson Apr 07 '12 at 16:02
  • Thanks for the suggestion. Other than the use of '_' (which in my case were to actually indicate instance variables), would you mind so kindly to point out a couple things, while I'm going through the docs on the web right now about the conventions? – Alex Ku Apr 07 '12 at 16:51

1 Answers1

15

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

Robin
  • 36,233
  • 5
  • 47
  • 99
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    This code shows me a JFileChooser where all files are greyed-out and not selectable, but still visible (OS X, JDK 1.7) +1 for the SSCCE, makes it very simple to test – Robin Apr 07 '12 at 16:25
  • @Robin: thanks for the heads up. On Windows 7, I don't see the files at all, just the directories. I wonder if this could be L&F dependent. – Hovercraft Full Of Eels Apr 07 '12 at 16:26
  • Thanks Hovercraft Full of Eels & Robin. Yes, I can still see the files on my mac, using your example. +1 on the SSCCE example and I'll definitely do that too in my future SO questions. (I was hoping I could post an image here but turns out I need 10 reputation point or more before I could do that on SO. Oops.) – Alex Ku Apr 07 '12 at 16:33
  • 2
    @HovercraftFullOfEels I added a screenshot to your post so others could clearly see that the `JFileChooser` behaves differently under OS X – Robin Apr 07 '12 at 16:34
  • @Alex: I've up-voted your original post as it was amazingly informative, well organized and with good code formatting for a first post. You should have your 10 rep points soon. – Hovercraft Full Of Eels Apr 07 '12 at 16:36
  • 2
    @trashgod - ah. so it's a mac dealio with this. Thanks! (copying your comment here for future reference) from http://stackoverflow.com/questions/2883447/jfilechooser-select-directory-but-show-files/2883595#2883595 "@mmyers: Empirically, it's platform-dependent, with files appearing gray in all supported L&Fs on Mac OS X. – trashgod May 21 '10 at 17:02" – Alex Ku Apr 07 '12 at 17:38
  • @AlexKu: I think so; Robin's screenshot is illustrative. – trashgod Apr 07 '12 at 20:02
  • Is there any workaround for Mac? I really don't want greyed-out files, just the selectable ones. – Igor F. Oct 05 '17 at 11:43
  • I've just run into this with an up to date Mac and am seeing the same problem. Files are visible and force me to do extra scrolling to get where I want. The problem showed up in my code and I've reproduced it just now with the Hovercraft SSCCE. I don't know of a solution. – Christopher Apr 14 '22 at 23:32