如何在OS X中将java FileDialog接受目录作为其FileType? [英] How can I make a java FileDialog accept directories as its FileType in OS X?

查看:115
本文介绍了如何在OS X中将java FileDialog接受目录作为其FileType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序在Mac上运行时,我正在尝试从使用JFileChooser切换到FileDialog,以便它将使用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. 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());

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

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而不是可移植的JFileChooser,则需要设置系统属性以便FileDialogs已创建用于目录。

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.

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

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

应该注意的是,这不是便携式的,因为你想要更换便携式JFileDialog,我认为这不是问题。

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.

这篇关于如何在OS X中将java FileDialog接受目录作为其FileType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆