简体   繁体   中英

How do I get JFileChooser to treat a folder as a directory?

I am trying to select all the files from a folder with a JFileChooser

   .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

but this doesn't let me select the folder it only lets me open it. So how would i be able to select a folder with JFileChooser and then input all the files in the selected folder without having to actually select each file individually because there might be a lot of file in the folder in the future. My whole code looks like this

  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");
        }
    }
  }
}                            

From your code, you don't seem to need to. Simply allow the user to select the directory you want to process and use File#listFiles to get it's contents

You would then iterate over this list and read each file, for example..

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");
        }
    }
}

Updated

The simple code below, allows me to select a directory and click Open , which will return the directory selected as the selected file when request...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM