JFileChooser 选择目录但显示文件

发布于 2024-09-02 14:36:45 字数 139 浏览 7 评论 0原文

我觉得应该有一个简单的方法来做到这一点,但我无法弄清楚。我有一个 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 技术交流群。

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

发布评论

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

评论(9

浮生未歇 2024-09-09 14:36:45

我的解决方案是合并camickr和trashgod的答案:

    final JFileChooser chooser = new JFileChooser() {
            public void approveSelection() {
                if (getSelectedFile().isFile()) {
                    return;
                } else
                    super.approveSelection();
            }
    };
    chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );

My solution is a merge between the answers of camickr and trashgod:

    final JFileChooser chooser = new JFileChooser() {
            public void approveSelection() {
                if (getSelectedFile().isFile()) {
                    return;
                } else
                    super.approveSelection();
            }
    };
    chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
明媚殇 2024-09-09 14:36:45

请参阅 setFileSelectionMode() 如何使用文件选择器

setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)

附录:取消注释此 FileChooserDemo,但它似乎与平台相关。

附录:如果使用 FILES_AND_DIRECTORIES,请考虑相应地更改按钮文本:

chooser.setApproveButtonText("Choose directory");

由于效果取决于 L&F,请考虑在已经满足您的 UI 要求的平台上使用 DIRECTORIES_ONLY

if (System.getProperty("os.name").startsWith("Mac OS X")) {
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
} else {
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}

See setFileSelectionMode() in How to Use File Choosers:

setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)

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:

chooser.setApproveButtonText("Choose directory");

As the effect is L&F dependent, consider using DIRECTORIES_ONLY on platforms that already meet your UI requirements:

if (System.getProperty("os.name").startsWith("Mac OS X")) {
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
} else {
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
段念尘 2024-09-09 14:36:45

重写approveSelection()方法。像这样的东西:

JFileChooser chooser = new JFileChooser( new File(".") )
{
    public void approveSelection()
    {
        if (getSelectedFile().isFile())
        {
            // beep
            return;
        }
        else
            super.approveSelection();
    }
};

Override the approveSelection() method. Something like:

JFileChooser chooser = new JFileChooser( new File(".") )
{
    public void approveSelection()
    {
        if (getSelectedFile().isFile())
        {
            // beep
            return;
        }
        else
            super.approveSelection();
    }
};
江城子 2024-09-09 14:36:45

覆盖 approveSelection 的解决方案可能会让某些用户感到烦恼。

有时,用户会无缘无故地单击目录中的文件(即使她想选择目录而不是文件)。如果发生这种情况,用户将(有点)陷入 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);
}

选择目录,即使用户选择了文件,我认为也会产生更好的可用性。

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 the approveSelection will fail, even if she deselects the file. To avoid this annoyance, this is what I do:

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.

好菇凉咱不稀罕他 2024-09-09 14:36:45

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

自由如风 2024-09-09 14:36:45

保留 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY) 并使用:

File[] selectedFiles = fileChooser.getSelectedFile().listFiles();

Keep the fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY) and use:

File[] selectedFiles = fileChooser.getSelectedFile().listFiles();
哭泣的笑容 2024-09-09 14:36:45

JFileChooser 支持三种选择模式:仅文件、仅目录以及文件和目录。在您的情况下,您需要的是:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

来源: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 :

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

source : http://www.java2s.com/Tutorial/Java/0240__Swing/TheJFileChoosersupportsthreeselectionmodesfilesonlydirectoriesonlyandfilesanddirectories.htm

ペ泪落弦音 2024-09-09 14:36:45

选择多个文件夹但显示所有包含的文件

    import javax.swing.*;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    
    public class MultipleFilesAndDirectoryChooserButDisplayFiles {
        public static void main(String[] args) {
            ArrayList<File> tempFiles = new ArrayList<>();
            ArrayList<File> finalFiles = new ArrayList<>();
            ArrayList<String> relativeFiles = new ArrayList<>();
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle("Choose File To Transfer");
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fileChooser.setMultiSelectionEnabled(true);
            int returnVal = fileChooser.showOpenDialog(null);
            fileChooser.approveSelection();
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                fileChooser.approveSelection();
                var fileAddress = fileChooser.getSelectedFiles();
                for (var arrElement : fileAddress) {
                    tempFiles.add(arrElement);
                    File baseFile;
                    baseFile = arrElement.getParentFile();
                    Iterator<File> iterator = tempFiles.iterator();
                    while (iterator.hasNext()) {
                        File file = iterator.next();
                        if (file.isDirectory()) {
                            var enclosedFiles = file.listFiles();
                            if (enclosedFiles != null) {
                                if (enclosedFiles.length != 0) {
                                    var index = tempFiles.indexOf(file);
                                    tempFiles.remove(file);
                                    tempFiles.addAll(index, Arrays.asList(enclosedFiles));
                                    iterator = tempFiles.iterator();
                                } else {
                                    tempFiles.remove(file);
                                    finalFiles.add(file);
                                    relativeFiles.add(baseFile.toURI().relativize(file.toURI()).getPath());
                                    iterator = tempFiles.iterator();
                                }
                            }
                        } else if (file.isFile()) {
                            tempFiles.remove(file);
                            finalFiles.add(file);
                            relativeFiles.add(baseFile.toURI().relativize(file.toURI()).getPath());
                            iterator = tempFiles.iterator();
                        }
                    }
    
    
                }
                for (var relativeFile : relativeFiles) {
                    System.out.println(relativeFile);
    
                }
                for (var file : finalFiles) {
                    System.out.println(file);
                }
    
            }
        }
    }

输出:

  • 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

    import javax.swing.*;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    
    public class MultipleFilesAndDirectoryChooserButDisplayFiles {
        public static void main(String[] args) {
            ArrayList<File> tempFiles = new ArrayList<>();
            ArrayList<File> finalFiles = new ArrayList<>();
            ArrayList<String> relativeFiles = new ArrayList<>();
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle("Choose File To Transfer");
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fileChooser.setMultiSelectionEnabled(true);
            int returnVal = fileChooser.showOpenDialog(null);
            fileChooser.approveSelection();
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                fileChooser.approveSelection();
                var fileAddress = fileChooser.getSelectedFiles();
                for (var arrElement : fileAddress) {
                    tempFiles.add(arrElement);
                    File baseFile;
                    baseFile = arrElement.getParentFile();
                    Iterator<File> iterator = tempFiles.iterator();
                    while (iterator.hasNext()) {
                        File file = iterator.next();
                        if (file.isDirectory()) {
                            var enclosedFiles = file.listFiles();
                            if (enclosedFiles != null) {
                                if (enclosedFiles.length != 0) {
                                    var index = tempFiles.indexOf(file);
                                    tempFiles.remove(file);
                                    tempFiles.addAll(index, Arrays.asList(enclosedFiles));
                                    iterator = tempFiles.iterator();
                                } else {
                                    tempFiles.remove(file);
                                    finalFiles.add(file);
                                    relativeFiles.add(baseFile.toURI().relativize(file.toURI()).getPath());
                                    iterator = tempFiles.iterator();
                                }
                            }
                        } else if (file.isFile()) {
                            tempFiles.remove(file);
                            finalFiles.add(file);
                            relativeFiles.add(baseFile.toURI().relativize(file.toURI()).getPath());
                            iterator = tempFiles.iterator();
                        }
                    }
    
    
                }
                for (var relativeFile : relativeFiles) {
                    System.out.println(relativeFile);
    
                }
                for (var file : finalFiles) {
                    System.out.println(file);
                }
    
            }
        }
    }

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

心意如水 2024-09-09 14:36:45

我认为最好的解决方案就是允许用户选择文件或目录。如果用户选择一个文件,则只需使用该文件所在的目录。

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.

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