简体   繁体   English

如何在OS X中将java FileDialog接受目录作为其FileType?

[英]How can I make a java FileDialog accept directories as its FileType in OS X?

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. 当我的应用程序在Mac上运行时,我试图从使用JFileChooser切换到FileDialog,以便它将使用OS X文件选择器。 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). 所以我的文件选择器将允许任何目录作为文件选择器的输入(后面的方法已经检查以确保该目录是我想让FileDialog接受任何目录的正确类型的目录)。

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. 假设您决定使用FileDialog而不是可移植的JFileChooser,则需要设置系统属性,以便为目录创建FileDialogs。

The property in question is apple.awt.fileDialogForDirectories . 有问题的属性是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. 应该注意的是,这不是便携式的,但是,因为您正在寻找替换便携式JFileDialog,我认为这不是问题。

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 当我的应用程序在mac上运行时,我试图从使用JFileChooser切换到FileDialog,以便它将使用OSx文件选择器

I would suggest that you try to stay in the Swing world and shy away from the heavier-weight world of AWT. 我建议你试着留在Swing世界,并避开重量级的AWT世界。 There are ways to work around issues with the Swing L&F on Macs, if that is what your problem is. 如果这就是你的问题,有办法解决Mac上的Swing L&F问题。 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. 看一下之前的问题 ,该问题链接到一个网站,该网站显示如何在文件选择器中获取正确的Mac图标。

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. 如果您还有其他原因希望继续使用FileDialog ,我很乐意删除此帖子。

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. 我无法解决本机FileDialog实现的按钮(仅限英语)的翻译。

So I get a workaround that works perfectly on macOS: 所以我得到一个在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! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM