如何使 java FileDialog 接受目录作为 OS X 中的文件类型?

发布于 2024-07-30 05:32:42 字数 429 浏览 6 评论 0原文

当我的应用程序在 Mac 上运行时,我试图从使用 JFileChooser 切换到 FileDialog,以便它将使用 OS X 文件选择器。 到目前为止,我有以下代码:

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

我会为这个问题输入什么? 这样我的文件选择器将允许任何目录作为文件选择器的输入(下面的方法已经检查以确保该目录是正确的目录类型,我只想让 FileDialog 接受任何目录)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御弟哥哥 2024-08-06 05:32:43

假设您决定使用 FileDialog 而不是便携式 JFileChooser,则需要设置系统属性,以便为目录创建 FileDialog。

有问题的属性是 apple.awt.fileDialogForDirectories

因此,只需执行以下操作:

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");

应该注意的是,这不是可移植的,但是,由于您正在寻找替换可移植的 JFileDialog,我认为这不是问题。

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.

短叹 2024-08-06 05:32:43

当我的应用程序在 Mac 上运行时,我尝试从使用 JFileChooser 切换到 FileDialog,以便它将使用 OSx 文件选择器

我建议您尝试留在 Swing 世界中并避开较重的重量级AWT 的世界。 如果您遇到问题,可以通过多种方法解决 Mac 上的 Swing L&F 问题。 看看这篇文章到一个早期的问题,它链接到一个网站,显示如何在文件选择器中获取正确的 Mac 图标。

请原谅我没有准确回答你的问题。 如果您还有其他原因希望继续使用 FileDialog,我很乐意删除这篇文章。

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.

‖放下 2024-08-06 05:32:43

使用最流行的解决方案一段时间后:

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

我无法解析本机 FileDialog 实现的按钮翻译(仅英文)。

所以我得到了一个在 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;

享受吧!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文