10

The JFileChooser in "directories only" mode on the Mac has two serious, crippling problems:

1) You cannot create directories with it

2) You cannot switch drives

This is rather a huge problem for my installer app. As far as I can tell, Apple provides no way around this problem, you can't even activate the non-native directory chooser ... so the only alternative is to find a free/open source pure-Java replacement widget.

Does anybody know of one?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Mike Hearn
  • 259
  • 2
  • 6
  • 1
    This is a duplicate: http://stackoverflow.com/questions/845403/how-can-i-make-a-jfilechooser-on-the-mac-that-lets-users-create-directories – Tom Aug 31 '09 at 07:43
  • ...except that the other question does not address the "switch drives" part. – Jonik Aug 31 '09 at 12:28
  • 1
    Hmm, both were asked by the same person apparently, with 3 month interval in between... – Jonik Aug 31 '09 at 12:30
  • Yes, that was me too. I registered this time. The last question died without an answer (unfortunately "it's a usability thing" doesn't help me). – Mike Hearn Aug 31 '09 at 20:49

4 Answers4

8

What about using java.awt.FileDialog? It shows a native file chooser and allows creating new folders.

public static void main(String[] args) throws UnsupportedLookAndFeelException {
    JFrame frame = new JFrame();
    System.setProperty("apple.awt.fileDialogForDirectories", "true");
    FileDialog d = new FileDialog(frame);
    d.setVisible(true);
}
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
  • That's for picking files, not directories. I don't think it's possible to select a directory with that widget. – Mike Hearn Aug 31 '09 at 20:48
  • 1
    I added the missing line to make it allow choosing folders. I use this in my commercial app to let users pick folders. It's much better than JFileChooser – Steve McLeod Sep 01 '09 at 07:56
  • I believe the behavior of `FileDialog` on OS X has changed. If you set `apple.awt.fileDialogForDirectories` to `true`, you actually disable the selection of files, so only directories may be selected. By not setting any system property, OS X will allow you to select either files or directories. – Christopher Schultz Jan 17 '19 at 18:30
2

I used JFileChooser with showDialog method and I did not have problem. I can create directories and sava as the file with the name that I like. If you use only showOpenDialog method you can not create directories

sahl
  • 21
  • 1
2

I discovered that there is a magic property you can set that makes the awt filepicker do the right thing:

System.setProperty("apple.awt.fileDialogForDirectories", "true");

I vaguely recall trying this before when I was on OS X 10.4 and it didn't work, but now I'm on Leopard and it does, so I'm a happy camper.

zellus
  • 9,617
  • 5
  • 39
  • 56
Mike Hearn
  • 259
  • 2
  • 6
2

JFileChooser can see external drives. Navigate down from the root into /Volumes and all drives are listed there. It's not elegant, but it works...

http://lists.apple.com/archives/java-dev///2008/Feb/msg00079.html