JFileChooser select directory but show files

后端 未结 9 1747
孤城傲影
孤城傲影 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:39

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

    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.

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

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

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