java 如何将 JFileChooser 限制为一个目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32529/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:48:21  来源:igfitidea点击:

How do I restrict JFileChooser to a directory?

javaswingjfilechooser

提问by Allain Lalonde

I want to limit my users to a directory and its sub directories but the "Parent Directory" button allows them to browse to an arbitrary directory.

我想将我的用户限制在一个目录及其子目录中,但“父目录”按钮允许他们浏览到任意目录。

How should I go about doing that?

我该怎么做呢?

采纳答案by McDowell

You can probably do this by setting your own FileSystemView.

您可以通过设置自己的FileSystemView来做到这一点。

回答by Allain Lalonde

Incase anyone else needs this in the future:

以防其他人将来需要这个:

class DirectoryRestrictedFileSystemView extends FileSystemView
{
    private final File[] rootDirectories;

    DirectoryRestrictedFileSystemView(File rootDirectory)
    {
        this.rootDirectories = new File[] {rootDirectory};
    }

    DirectoryRestrictedFileSystemView(File[] rootDirectories)
    {
        this.rootDirectories = rootDirectories;
    }

    @Override
    public File createNewFolder(File containingDir) throws IOException
    {       
        throw new UnsupportedOperationException("Unable to create directory");
    }

    @Override
    public File[] getRoots()
    {
        return rootDirectories;
    }

    @Override
    public boolean isRoot(File file)
    {
        for (File root : rootDirectories) {
            if (root.equals(file)) {
                return true;
            }
        }
        return false;
    }
}

You'll obviously need to make a better "createNewFolder" method, but this does restrict the user to one of more directories.

您显然需要制作更好的“createNewFolder”方法,但这确实将用户限制在多个目录之一。

And use it like this:

并像这样使用它:

FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("X:\"));
JFileChooser fileChooser = new JFileChooser(fsv);

or like this:

或者像这样:

FileSystemView fsv = new DirectoryRestrictedFileSystemView( new File[] {
    new File("X:\"),
    new File("Y:\")
});
JFileChooser fileChooser = new JFileChooser(fsv);

回答by mlh

The solution of Allain is almost complete. Three problems are open to solve:

阿兰的解决方案基本完成。三个问题有待解决:

  1. Clicking the "Home"-Button kicks the user out of restrictions
  2. DirectoryRestrictedFileSystemView is not accessible outside the package
  3. Starting point is not Root
  1. 单击“主页”按钮将用户踢出限制
  2. DirectoryRestrictedFileSystemView 在包外不可访问
  3. 起点不是Root


  1. Append @Override to DirectoryRestrictedFileSystemView
  1. 将 @Override 附加到 DirectoryRestrictedFileSystemView

public TFile getHomeDirectory() { return rootDirectories[0]; }

public TFile getHomeDirectory() { return rootDirectories[0]; }

  1. set class and constructor public

  2. Change JFileChooser fileChooser = new JFileChooser(fsv);into JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);

  1. 设置类和构造函数 public

  2. 更改JFileChooser fileChooser = new JFileChooser(fsv);JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);

I use it for restricting users to stay in a zip-file via TrueZips TFileChooser and with slight modifications to the above code, this works perfectly. Thanks a lot.

我用它来限制用户通过 TrueZips TFileChooser 留在一个 zip 文件中,并对上面的代码稍作修改,这完美地工作。非常感谢。

回答by Siddharth Tyagi

No need to be that complicated. You can easily set selection mode of a JFileChooser like this

没必要那么复杂。您可以像这样轻松设置 JFileChooser 的选择模式

JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setMultiSelectionEnabled(false);

You can read more reference here How to Use File Choosers

您可以在此处阅读更多参考如何使用文件选择器