如何使 java FileDialog 接受目录作为 OS X 中的文件类型?
当我的应用程序在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您决定使用 FileDialog 而不是便携式 JFileChooser,则需要设置系统属性,以便为目录创建 FileDialog。
有问题的属性是
apple.awt.fileDialogForDirectories
。
因此,只需执行以下操作:
应该注意的是,这不是可移植的,但是,由于您正在寻找替换可移植的 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:
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.
我建议您尝试留在 Swing 世界中并避开较重的重量级AWT 的世界。 如果您遇到问题,可以通过多种方法解决 Mac 上的 Swing L&F 问题。 看看这篇文章到一个早期的问题,它链接到一个网站,显示如何在文件选择器中获取正确的 Mac 图标。
请原谅我没有准确回答你的问题。 如果您还有其他原因希望继续使用
FileDialog
,我很乐意删除这篇文章。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.使用最流行的解决方案一段时间后:
我无法解析本机 FileDialog 实现的按钮翻译(仅英文)。
所以我得到了一个在 macOS 上完美运行的解决方法:
享受吧!
After using most popular solution for while:
I can't resolve translation of Buttons (only in English) of native FileDialog implementation.
So I get a workaround that works perfectly on macOS:
Enjoy!