JFileChooser select directory but show files

后端 未结 9 1746
孤城傲影
孤城傲影 2020-12-11 00:49

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 file

相关标签:
9条回答
  • 2020-12-11 01:28

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

    File[] selectedFiles = fileChooser.getSelectedFile().listFiles();
    
    0 讨论(0)
  • 2020-12-11 01:29

    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.

    0 讨论(0)
  • 2020-12-11 01:29

    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

    0 讨论(0)
  • 2020-12-11 01:38

    Override the approveSelection() method. Something like:

    JFileChooser chooser = new JFileChooser( new File(".") )
    {
        public void approveSelection()
        {
            if (getSelectedFile().isFile())
            {
                // beep
                return;
            }
            else
                super.approveSelection();
        }
    };
    
    0 讨论(0)
  • 2020-12-11 01:38

    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 );
    
    0 讨论(0)
  • 2020-12-11 01:38

    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

    0 讨论(0)
提交回复
热议问题