【问题标题】:How do I get JFileChooser to treat a folder as a directory?如何让 JFileChooser 将文件夹视为目录?
【发布时间】:2015-12-15 22:56:08
【问题描述】:

我正在尝试使用 JFileChooser 从文件夹中选择所有文件

   .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

但这不允许我选择文件夹,它只能让我打开它。那么我如何能够使用 JFileChooser 选择一个文件夹,然后输入所选文件夹中的所有文件,而不必实际单独选择每个文件,因为将来文件夹中可能会有很多文件。我的整个代码是这样的

  public class PicTest 
  {
   public static void main(String args[])
   {
    File inFile,dir;
    File[] list;
    Image pic[] = new Image[50];
    JFileChooser choose = new JFileChooser();
    choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int status = choose.showOpenDialog(null);
    if(status == JFileChooser.APPROVE_OPTION)
    {
        dir = choose.getCurrentDirectory();
        try
        {
            inFile = new File(choose.getSelectedFile().getAbsolutePath());
            list = dir.listFiles();
            for(int i=0; i < pic.length-1; i++)
            {
                BufferedImage buff = ImageIO.read(inFile);
                pic[i] = buff;
            }
        }
        catch(IOException e) 
        {
            System.out.println("Error");
        }
    }
  }
}                            

【问题讨论】:

  • 假设您不想更改选择,那么只需使用dir.listFiles()
  • 你试过了吗? setFileSelectionMode (JFileChooser .FILES_AND_DIRECTORIES)
  • 看看my answer 对另一个问题是否有帮助。
  • 我考虑过使用 .FILES_AND_DIRECTORIES 但我想做的是让用户能够选择一个文件夹,然后我想读入该文件夹中的所有文件。在这种情况下,文件夹将包含图像

标签: java swing file-io jfilechooser


【解决方案1】:

根据您的代码,您似乎不需要。只需让用户选择您要处理的目录并使用File#listFiles 来获取它的内容

然后您将遍历此列表并读取每个文件,例如..

Image pic[] = null;
JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int status = choose.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
    File dir = choose.getCurrentDirectory();
    if (dir.exists()) {
        File[] list = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                String name = pathname.getName().toLowerCase();
                return name.endsWith(".png")
                        || name.endsWith(".jpg")
                        || name.endsWith(".jpeg")
                        || name.endsWith(".bmp")
                        || name.endsWith(".gif");
            }
        });
        try {
            // Only now do you know the length of the array
            pic = new Image[list.length];
            for (int i = 0; i < pic.length; i++) {
                BufferedImage buff = ImageIO.read(list[i]);
                pic[i] = buff;
            }
        } catch (IOException e) {
            System.out.println("Error");
        }
    }
}

更新

下面的简单代码,允许我选择一个目录并单击打开,这将在请求时返回选择的目录作为选定文件...

JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (choose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
    System.out.println(choose.getSelectedFile());
}

【讨论】:

  • 但这仍然不能让我选择文件夹,它只能让我打开文件夹并选择其中的内容。
  • 选择你想要的目录,按Open,它将关闭对话框,getSelectedFile方法将返回一个File对该目录的引用。似乎只为我工作
猜你喜欢
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多