JFileChooser 选择目录但显示文件
我觉得应该有一个简单的方法来做到这一点,但我无法弄清楚。我有一个 JFileChooser 允许用户选择目录。我想显示目录中的所有文件,以便为用户提供一些上下文,但只有目录应该被接受作为选择(也许选择文件时“打开”按钮将被禁用)。有一个简单的方法可以做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我的解决方案是合并camickr和trashgod的答案:
My solution is a merge between the answers of camickr and trashgod:
请参阅
setFileSelectionMode()
如何使用文件选择器:附录:取消注释此
FileChooserDemo
,但它似乎与平台相关。附录:如果使用
FILES_AND_DIRECTORIES
,请考虑相应地更改按钮文本:由于效果取决于 L&F,请考虑在已经满足您的 UI 要求的平台上使用
DIRECTORIES_ONLY
:See
setFileSelectionMode()
in How to Use File Choosers:Addendum: The effect can be see by uncommenting line 73 of this
FileChooserDemo
, but it appears to be platform-dependent.Addendum: If using
FILES_AND_DIRECTORIES
, consider changing the button text accordingly:As the effect is L&F dependent, consider using
DIRECTORIES_ONLY
on platforms that already meet your UI requirements:重写approveSelection()方法。像这样的东西:
Override the approveSelection() method. Something like:
覆盖
approveSelection
的解决方案可能会让某些用户感到烦恼。有时,用户会无缘无故地单击目录中的文件(即使她想选择目录而不是文件)。如果发生这种情况,用户将(有点)陷入 JFileChooser 中,因为即使她取消选择文件,approveSelection 也会失败。为了避免这种烦恼,我这样做:
选择目录,即使用户选择了文件,我认为也会产生更好的可用性。
The solution of overriding
approveSelection
can be annoying for certain users.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
JFileChooser
as theapproveSelection
will fail, even if she deselects the file. To avoid this annoyance, this is what I do:Selecting the directory, even when the user selected a file results in a better usability in my opinion.
AFAIK JFileChooser 将文件过滤(可以查看什么,非常可配置)与选择过滤(可以选择什么)分开。
选择过滤的配置更加有限,但据我所知,您可以选择仅允许选择目录或文件 setFileSelectionMode()
AFAIK JFileChooser separates file filtering (what can be viewed, very configurable) from selection filtering (what can be chosen).
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()
保留 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY) 并使用:
Keep the
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
and use:JFileChooser 支持三种选择模式:仅文件、仅目录以及文件和目录。在您的情况下,您需要的是:
来源:http://www.java2s。 com/Tutorial/Java/0240__Swing/TheJFileChoosersupportstwoselectionmodesfilesonlydirectoriesonlyandfilesanddirectories.htm
The JFileChooser supports three selection modes: files only, directories only, and files and directories. In your case what you need is :
source : http://www.java2s.com/Tutorial/Java/0240__Swing/TheJFileChoosersupportsthreeselectionmodesfilesonlydirectoriesonlyandfilesanddirectories.htm
选择多个文件夹但显示所有包含的文件
输出:
Folder1/EmptyFolder/
文件夹1/子文件夹1/1.1.txt
文件夹1/子文件夹1/1.2.txt
文件夹1/子文件夹1/1.3.txt
文件夹1/子文件夹1/子文件夹1.1/1.1.1.txt
文件夹1/子文件夹1/子文件夹1.1/1.2.1.txt
文件夹1/子文件夹1/子文件夹1.1/1.3.1.txt
文件夹1/子文件夹2/2.1/2.1.1.txt
文件夹1/子文件夹2/2.1/2.1.2.txt
文件夹1/子文件夹2/2.1/2.1.3.txt
文件夹1/子文件夹3/3.1.txt
文件夹1/子文件夹3/3.2.txt
文件夹1/子文件夹3/3.3.txt
文件夹2/子文件夹/2.1.txt
文件夹2/子文件夹/空文件夹/
file1.txt
文件2.txt
E:\Folder1\EmptyFolder
E:\Folder1\SubFolder1\1.1.txt
E:\Folder1\SubFolder1\1.2.txt
E:\Folder1\SubFolder1\1.3.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.1.1.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.2.1.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.3.1.txt
E:\Folder1\SubFolder2\2.1\2.1.1.txt
E:\Folder1\SubFolder2\2.1\2.1.2.txt
E:\Folder1\SubFolder2\2.1\2.1.3.txt
E:\Folder1\SubFolder3\3.1.txt
E:\Folder1\SubFolder3\3.2.txt
E:\Folder1\SubFolder3\3.3.txt
E:\Folder2\子文件夹\2.1.txt
E:\Folder2\子文件夹\EmptyFolder
E:\file1.txt
E:\file2.txt
Select Multiple Folders But Show All Included files
Output:
Folder1/EmptyFolder/
Folder1/SubFolder1/1.1.txt
Folder1/SubFolder1/1.2.txt
Folder1/SubFolder1/1.3.txt
Folder1/SubFolder1/SubFolder 1.1/1.1.1.txt
Folder1/SubFolder1/SubFolder 1.1/1.2.1.txt
Folder1/SubFolder1/SubFolder 1.1/1.3.1.txt
Folder1/SubFolder2/2.1/2.1.1.txt
Folder1/SubFolder2/2.1/2.1.2.txt
Folder1/SubFolder2/2.1/2.1.3.txt
Folder1/SubFolder3/3.1.txt
Folder1/SubFolder3/3.2.txt
Folder1/SubFolder3/3.3.txt
Folder2/Sub Folder/2.1.txt
Folder2/Sub Folder/EmptyFolder/
file1.txt
file2.txt
E:\Folder1\EmptyFolder
E:\Folder1\SubFolder1\1.1.txt
E:\Folder1\SubFolder1\1.2.txt
E:\Folder1\SubFolder1\1.3.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.1.1.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.2.1.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.3.1.txt
E:\Folder1\SubFolder2\2.1\2.1.1.txt
E:\Folder1\SubFolder2\2.1\2.1.2.txt
E:\Folder1\SubFolder2\2.1\2.1.3.txt
E:\Folder1\SubFolder3\3.1.txt
E:\Folder1\SubFolder3\3.2.txt
E:\Folder1\SubFolder3\3.3.txt
E:\Folder2\Sub Folder\2.1.txt
E:\Folder2\Sub Folder\EmptyFolder
E:\file1.txt
E:\file2.txt
我认为最好的解决方案就是允许用户选择文件或目录。如果用户选择一个文件,则只需使用该文件所在的目录。
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.