Search code examples
javaimageswingjfilechooserfilefilter

Starting a JFileChooser at a specified directory and only showing files of a specific type


I have a program utilizing a JFileChooser. To be brief, the full program is a GUI which allows users to manipulate PNGs and JPGs. I would like to make it so that the JFileChooser instantly opens to the picture directory (windows). When the user opens their JFileChooser, it would open directly to the pictures library C:\Users\(USER)\Pictures

Furthermore, it would be nice to ONLY show files of a specific type (PNGs and JPGs). Many programs seem to be able to do this; only allowing selection of specific files. Does JFileChooser allow such a thing? Currently, I am using a massively unreliable, run around method to reject non-PNGs/JPGs.

The following refers to the "browse" button of the GUI, in which a user will select their picture for editing and it will display it on the screen.

    try {
       int val = filec.showOpenDialog(GridCreator.this);
       if(val==JFileChooser.APPROVE_OPTION) {
          File unfiltered_picture = filec.getSelectedFile();
          //get the extension of the file
          extension=unfiltered_picture.getPath();
          int index=extension.indexOf(".");
          extension=extension.substring(index+1, extension.length());
          //if the file is not jpg, png, or jpeg, reject it and send a message to the user.
          if(!extension.matches("[jJ][pP][gG]") && !extension.matches("[pP][nN][gG]") && !extension.matches("[jJ][pP][eE][gG]")) {
             JOptionPane.showMessageDialog(null,
                                           "cannot load file. File must be of type png, jpeg, or jpg. \n Your file is of type " + extension,
                                            "Error: improper file",
                                            JOptionPane.OK_OPTION);
           //if the file is of the proper type, display it to the user on the img JLabel.
           } else {
              finalImage = ImageIO.read(unfiltered_picture);
              ImageIcon imgIcon = new ImageIcon();
              imgIcon.setImage(finalImage);
              img.setIcon(imgIcon);
              img.invalidate();
              h_divide.setValue(0);
              v_divide.setValue(0);
           }
       }
   } catch(IOException exception) {
        exception.printStackTrace();
   }

Thank you.


Solution

  • You need to construct your JFileChooser with the directory you want to start in and then pass a FileFilter into it before setting visible.

        final JFileChooser fileChooser = new JFileChooser(new File("File to start in"));
        fileChooser.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }
                final String name = f.getName();
                return name.endsWith(".png") || name.endsWith(".jpg");
            }
    
            @Override
            public String getDescription() {
                return "*.png,*.jpg";
            }
        });
        fileChooser.showOpenDialog(GridCreator.this);
    

    This example filters for files ending in ".png" or ".jpg".