如何将 JFileChooser 限制为目录?

发布于 2024-07-04 16:06:09 字数 66 浏览 5 评论 0原文

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

我应该怎样做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我要还你自由 2024-07-11 16:06:09

万一其他人将来需要这个:

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;
    }
}

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

并像这样使用它:

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

或像这样:

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

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.

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);
猫七 2024-07-11 16:06:09

您可以通过设置自己的 FileSystemView< 来完成此操作/a>.

You can probably do this by setting your own FileSystemView.

肤浅与狂妄 2024-07-11 16:06:09

阿兰的解决方案已经基本完成了。 三个问题需要解决:

  1. 单击“Home”按钮将用户踢出限制
  2. DirectoryRestrictedFileSystemView 无法在包外部访问
  3. 起始点不是 Root

  1. 将 @Override 附加到 DirectoryRestrictedFileSystemView

public TFile getHomeDirectory()
{
返回根目录[0];
}

  1. 设置类和构造函数 public

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

我用它来限制用户通过 TrueZips TFileChooser 保留在 zip 文件中,并对上述代码稍加修改,这可以工作完美。 多谢。

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. Append @Override to DirectoryRestrictedFileSystemView

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

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.

鹿港小镇 2024-07-11 16:06:09

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

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

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

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

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

You can read more reference here How to Use File Choosers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文