macos 如何让 Java FileDialog 在 OS X 中接受目录作为其 FileType?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a java FileDialog accept directories as its FileType in OS X?
提问by Mike2012
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:
当我的应用程序在 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());
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 接受任何目录)。
回答by Ben S
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,您需要设置系统属性,以便创建的 FileDialog 用于目录。
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,我认为这不是问题。
回答by akf
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. 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.
我建议你尽量留在 Swing 世界,避开重量级的 AWT 世界。如果这就是您的问题,有多种方法可以解决 Mac 上的 Swing L&F 问题。看一看这篇关于较早问题的帖子,该帖子链接到一个网站,该网站展示了如何在文件选择器中获取正确的 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
,我很乐意删除此帖子。
回答by Dyorgio
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!
享受!