How can I make a java FileDialog accept directories as its FileType in OS X?
Asked Answered
F

3

10

I am trying to switch from using a JFileChooser to a FileDialog when my app is being run on a mac so that it will use the OS X file chooser. So far I have the following code:

    FileDialog fd = new FileDialog(this);
    fd.setDirectory(_projectsBaseDir.getPath());
    fd.setLocation(50,50);
    fd.setFile(?);
    fd.setVisible(true);
    File selectedFile = new File(fd.getFile());

What would I put in for the question ? so that my file chooser would allow any directory to be the input for file chooser (the method that follows already checks to make sure that the directory is the right kind of directory I just want to the FileDialog to accept any directory).

Fu answered 3/8, 2009 at 21:24 Comment(0)
I
12

Assuming you're determined to use the FileDialog instead of the portable JFileChooser, you need to set the system property so that FileDialogs created are for directories.

The property in question is apple.awt.fileDialogForDirectories.

So simply do the following:

System.setProperty("apple.awt.fileDialogForDirectories", "true");
FileDialog fd = new FileDialog(this); 
fd.setDirectory(_projectsBaseDir.getPath()); 
fd.setLocation(50,50);
fd.setVisible(true); 
File selectedFile = new File(fd.getFile());
System.setProperty("apple.awt.fileDialogForDirectories", "false");

It should be noted that this isn't portable, however, since you're looking to replace the portable JFileDialog, I assume that's not an issue.

Iceni answered 3/8, 2009 at 21:35 Comment(1)
Is there an equivalent for Windows?Witkin
A
1

I am trying to switch from using a JFileChooser to a FileDialog when my app is being run on a mac so that it will use the OSx file chooser

I would suggest that you try to stay in the Swing world and shy away from the heavier-weight world of AWT. There are ways to work around issues with the Swing L&F on Macs, if that is what your problem is. Take a look at this post to an earlier question, which links to a site that shows how to get the correct Mac icons in the file chooser.

Excuse me for not exactly answering your question. If there are other reasons why you would prefer to stay with FileDialog, I will gladly remove this post.

Asthmatic answered 3/8, 2009 at 21:36 Comment(2)
I agree - the JFiloeChooser is a much better option, and his question sounds more like a L&F problem than anything else.Ramonaramonda
The FileDialog shows the platform's file chooser on MacOS (at least these days) including the sidebar and the various folder options (e.g. New Folder, Search, etc.). It's pretty much superior in ever way except for the inability to allow the programmer allow the selection of files and/or folders conveniently.Studied
T
1

After using most popular solution for while:

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

I can't resolve translation of Buttons (only in English) of native FileDialog implementation.

So I get a workaround that works perfectly on macOS:

try {
    Process process = Runtime.getRuntime().exec(new String[]{//
        "/usr/bin/osascript", //
        "-e", //
        "set selectedFolder to choose folder\n"//
        + "return POSIX path of selectedFolder"
    });
    int result = process.waitFor();
    if (result == 0) {
        String selectedFolder = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
        return new File(selectedFolder);
    }
} catch (Exception ex) {
}

return null;

Enjoy!

Telemotor answered 17/9, 2017 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.