-2

I can't open a file by just writing the file's name in the JFileChooser unless I'm in the same folder as it, what can I do to fix it?

    JDialog.setDefaultLookAndFeelDecorated(true);
                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    FileFilter imageFilter = new FileNameExtensionFilter("Image files", ImageIO.getReaderFileSuffixes());
                    chooser.setFileFilter(imageFilter);
                    File file = chooser.getSelectedFile();
                    String filePath = file.getAbsolutePath();
                    icon = new ImageIcon(filePath);
                    try{
                        original = ImageIO.read(file);
                        image = ImageIO.read(file);
                        width = image.getWidth();
                        height = image.getHeight();
                        if (width > 1000 && height > 1000){
                            image = null;
                            JOptionPane.showMessageDialog(null, "Image is too big (maximum 1000px by 1000px)", "Message: ", JOptionPane.INFORMATION_MESSAGE);
                        } else if (width > 1000 && height <= 1000) {
                            image = null;
                            JOptionPane.showMessageDialog(null, "Width is too big (maximum 1000px)", "Message: ", JOptionPane.INFORMATION_MESSAGE);
                        } else if (width <= 1000 && height > 1000) {
                            image = null;
                            JOptionPane.showMessageDialog(null, "Height is too big (maximum 1000px)", "Message: ", JOptionPane.INFORMATION_MESSAGE);
                        }
                    } catch (IOException e) {
                        System.out.println(e);
                    }

                    picture = new ImageIcon(image);
                    label.setIcon(picture);
                }
J. Doe
  • 1
  • 1

1 Answers1

0

Just the filename will not work if you are not in the directory because the file will be searched for in the current directory in the opened JFileChooser. You will need to provide the JFileChooser with the complete absolute path to the file instead of just the file name

NOTE: i just simulate it in Notepad++ it just says the filename cannot be found, so you can check for the file existence before other operation, add this snippet to your code

JDialog.setDefaultLookAndFeelDecorated(true);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        FileFilter imageFilter = new FileNameExtensionFilter("Image files", ImageIO.getReaderFileSuffixes());
        chooser.setFileFilter(imageFilter);
        File file = chooser.getSelectedFile();
        if (!file.exists()) {
           JOptionPane.showMessageDialog(null, "The file "+file.getName()+"\ncannot be found !!!", JOptionPane.INFORMATION_MESSAGE);
           showDialogAgain();
        } else {
          String filePath = file.getAbsolutePath();
          icon = new ImageIcon(filePath);
          ...
        }
    }
    ...

Read more on the JFileChooser documentation

Thecarisma
  • 1,424
  • 18
  • 19