如何在Mac上创建一个允许用户创建目录的JFileChooser?

时间:2021-04-04 20:49:40

I have an installer program that lets the user choose a directory in which to install. The JFileChooser implementation on MacOS uses a native dialog (or at least it looks native). That's great.

我有一个安装程序,允许用户选择要安装的目录。 MacOS上的JFileChooser实现使用本机对话框(或者至少它看起来是原生的)。那很棒。

The only problem is there's no way to create a directory from this dialog ... you can only choose a pre-existing one, which is clunky. Is there a way to get this functionality?

唯一的问题是没有办法从这个对话框创建一个目录...你只能选择一个现有的目录,这是笨重的。有没有办法获得这个功能?

I use the JFileChooser in "select directories only" mode. Thus it isn't the same dialog as the usual MacOS file picker which does have that functionality.

我在“仅选择目录”模式下使用JFileChooser。因此,它与通常具有该功能的MacOS文件选择器不同。

2 个解决方案

#1


That is correct that the showOpenDialog method will not give you an option to create new folders. This is a usability thing as it does not really make sense to open something that does not exist. If you use the showSaveDialog there will be a button 'Make new Folder' or similar to that.

这是正确的,showOpenDialog方法不会为您提供创建新文件夹的选项。这是一个可用性的东西,因为打开一些不存在的东西并没有多大意义。如果你使用showSaveDialog,会有一个“Make new Folder”按钮或类似的按钮。

#2


public static void main(String[] args) {
    JFrame frame = new JFrame();
    FileDialog d = new FileDialog(frame);
    d.setVisible(true);
}

#1


That is correct that the showOpenDialog method will not give you an option to create new folders. This is a usability thing as it does not really make sense to open something that does not exist. If you use the showSaveDialog there will be a button 'Make new Folder' or similar to that.

这是正确的,showOpenDialog方法不会为您提供创建新文件夹的选项。这是一个可用性的东西,因为打开一些不存在的东西并没有多大意义。如果你使用showSaveDialog,会有一个“Make new Folder”按钮或类似的按钮。

#2


public static void main(String[] args) {
    JFrame frame = new JFrame();
    FileDialog d = new FileDialog(frame);
    d.setVisible(true);
}