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
How do I restrict JFileChooser to a directory?
提问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:
阿兰的解决方案基本完成。三个问题有待解决:
- Clicking the "Home"-Button kicks the user out of restrictions
- DirectoryRestrictedFileSystemView is not accessible outside the package
- Starting point is not Root
- 单击“主页”按钮将用户踢出限制
- DirectoryRestrictedFileSystemView 在包外不可访问
- 起点不是Root
- Append @Override to DirectoryRestrictedFileSystemView
- 将 @Override 附加到 DirectoryRestrictedFileSystemView
public TFile getHomeDirectory()
{
return rootDirectories[0];
}
public TFile getHomeDirectory()
{
return rootDirectories[0];
}
set class and constructor
publicChange
JFileChooser fileChooser = new JFileChooser(fsv);intoJFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);
设置类和构造函数
public更改
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
您可以在此处阅读更多参考如何使用文件选择器

