1

I am trying to use a FileDialog file chooser because I really need java app to have the native apple file chooser (I know we all hate hate the lack of portability but this is what I need). I am trying to make my file chooser allow the user to pick files that end with .ws. Here is what I tried:

            FileDialog fd = new         

           FileDialog(_sharedInstance,rsc.str("480"),FileDialog.LOAD);
           // fd.setFile("*.ws");
            class WSFilter implements FilenameFilter {
                public boolean accept(File dir, String name) {
                    return (name.endsWith(".ws"));
                }
            };
            FilenameFilter wsFilter = new WSFilter();

            fd.setFilenameFilter(wsFilter);
            fd.setDirectory(_projectsBaseDir.getPath());
            fd.setLocation(50,50);

           // fd.setFile("*");
            fd.setVisible(true);

For some reason my file chooser won't allow me to pick any files. Any ideas?

Mike2012
  • 7,629
  • 15
  • 84
  • 135

3 Answers3

3

Answer was I need this call: System.setProperty("apple.awt.fileDialogForDirectories", "false");

Mike2012
  • 7,629
  • 15
  • 84
  • 135
  • Is there any relevant documentation you can link to? – Michael Myers Aug 07 '09 at 21:24
  • Not really. In the following thread someone explained to me how that you need to set that global property in order to allow FileDialog to accept diectories, I had just forgotten to set it back. This is one of the many reasons people will tell you not to use FileDialog. http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x – Mike2012 Aug 10 '09 at 21:53
1

Why not use JFileChooser?

JFileChooser fileChooser = new JFileChooser(new File(filename));
fileChooser.addChoosableFileFilter(new MyFilter());

class MyFilter extends javax.swing.filechooser.FileFilter {
    public boolean accept(File file) {
        String filename = file.getName();
        return filename.endsWith(".java");
    }
    public String getDescription() {
        return "*.java";
    }
}
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
1

Haven't this been asked before?

Anyway, you may try to change L&F and keep using JFileChooser.

I've heard this one is good:

Quaqua Look and Feel

alt text

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • We are currently using quaqua but many mac users feel that it is not an adequate interpretation of the Mac GUI so I have been tasked to implement the file choosers to use the native file chooser. – Mike2012 Aug 07 '09 at 21:00
  • mmhh I see, depending on how relevant this is you may either implement your own subclass and add the missing parts ( which I think it would be quite hard ) or you may create a small native application which returns the filepath when invoked. That shouldn't be too hard to do ( when you know Objective-C :P ) T – OscarRyz Aug 07 '09 at 21:03