views:

428

answers:

3

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?

A: 

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
Because I need to use the mac native file chooser and JFileChooser does not allow this.
Mike2012
A: 
OscarRyz
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
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
+1  A: 

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

Mike2012
Is there any relevant documentation you can link to?
Michael Myers
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