Java JFileChooser 选择目录但显示文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2883447/
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
JFileChooser select directory but show files
提问by Jeff Storey
I feel like there should be a simple way to do this but I can't figure it out. I have a JFileChooser that allows the user to select directories. I want to show all the files in the directories to give the user some context, but only directories should be accepted as selections (maybe the Open button would be disabled when a file is selected). Is there an easy way of doing this?
我觉得应该有一个简单的方法来做到这一点,但我无法弄清楚。我有一个 JFileChooser 允许用户选择目录。我想显示目录中的所有文件以为用户提供一些上下文,但只应接受目录作为选择(也许选择文件时打开按钮将被禁用)。有没有简单的方法来做到这一点?
采纳答案by camickr
Override the approveSelection() method. Something like:
覆盖批准选择()方法。就像是:
JFileChooser chooser = new JFileChooser( new File(".") )
{
public void approveSelection()
{
if (getSelectedFile().isFile())
{
// beep
return;
}
else
super.approveSelection();
}
};
回答by Uri
AFAIK JFileChooser separates file filtering (what can be viewed, very configurable) from selection filtering (what can be chosen).
AFAIK JFileChooser 将文件过滤(可以查看的内容,非常可配置)与选择过滤(可以选择的内容)分开。
The configuration of selection filtering is much more limited, but AFAIK you can choose to allow only dirs or only files to be selected with setFileSelectionMode()
选择过滤的配置受到更多限制,但是AFAIK您可以选择只允许目录或只允许使用setFileSelectionMode()选择文件
回答by Martin Tilsted
I think the best solution is just to allow the user to select either a file or a directory. And if the user select a file just use the directory where that file is located.
我认为最好的解决方案是允许用户选择文件或目录。如果用户选择一个文件,只需使用该文件所在的目录。
回答by trashgod
See setFileSelectionMode()in How to Use File Choosers:
见setFileSelectionMode()在如何使用文件挑肥拣瘦:
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
Addendum: The effect can be see by uncommenting line 73 of this FileChooserDemo, but it appears to be platform-dependent.
附录:通过取消注释 this 的第 73 行可以看到效果FileChooserDemo,但它似乎与平台有关。
Addendum: If using FILES_AND_DIRECTORIES, consider changing the button text accordingly:
附录:如果使用FILES_AND_DIRECTORIES,请考虑相应地更改按钮文本:
chooser.setApproveButtonText("Choose directory");
As the effect is L&F dependent, consider using DIRECTORIES_ONLYon platforms that already meet your UI requirements:
由于效果取决于 L&F,请考虑DIRECTORIES_ONLY在已经满足您的 UI 要求的平台上使用:
if (System.getProperty("os.name").startsWith("Mac OS X")) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
} else {
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
回答by ablaeul
My solution is a merge between the answers of camickr and trashgod:
我的解决方案是将 camickr 和trashgod 的答案合并:
final JFileChooser chooser = new JFileChooser() {
public void approveSelection() {
if (getSelectedFile().isFile()) {
return;
} else
super.approveSelection();
}
};
chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
回答by Ankit
The solution of overriding approveSelectioncan be annoying for certain users.
approveSelection对于某些用户来说,覆盖的解决方案可能很烦人。
Sometimes, a user would just click on a file in a directory for no reason (even though she wants to select the directory and not the file). If that happens, the user would be (kind-a) stuck in the JFileChooseras the approveSelectionwill fail, even if she deselects the file. To avoid this annoyance, this is what I do:
有时,用户会无缘无故地单击目录中的文件(即使她想选择目录而不是文件)。如果发生这种情况, 即使她取消选择文件,用户也会(有点)陷入困境,JFileChooser因为这approveSelection将失败。为了避免这种烦恼,这就是我所做的:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_AND_DIRECTORIES);
int option = fileChooser.showDialog(null,
"Select Directory");
if (option == JFileChooser.APPROVE_OPTION) {
File f = fileChooser.getSelectedFile();
// if the user accidently click a file, then select the parent directory.
if (!f.isDirectory()) {
f = f.getParentFile();
}
System.out.println("Selected directory for import " + f);
}
Selecting the directory, even when the user selected a file results in a better usability in my opinion.
在我看来,即使用户选择了一个文件,选择目录也会导致更好的可用性。
回答by alexandre1985
Keep the fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)and use:
保留fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)并使用:
File[] selectedFiles = fileChooser.getSelectedFile().listFiles();
回答by Mehdi
The JFileChooser supports three selection modes: files only, directories only, and files and directories. In your case what you need is :
JFileChooser 支持三种选择模式:仅文件、仅目录和文件和目录。在您的情况下,您需要的是:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
来源:http: //www.java2s.com/Tutorial/Java/0240__Swing/TheJFileChoosersupportsthreeselectionmodesfilesonlydirectoriesonlyandfilesanddirectories.htm

