JFileChooser check if really a File is selected?

Viewed 2165

I noticed the following: If I start my application and go to open a file, then the JFileChooser appears. I tried writing a filename and I did not get an error, even though the file does not exist. What I get is a file path.

 JFileChooser fileChooser = new JFileChooser();
    int values = fileChooser.showOpenDialog(null);
    File file = fileChooser.getSelectedFile();

    if (values == JFileChooser.APPROVE_OPTION) {
        System.out.println(file.getPath());
    } else if (values == JFileChooser.CANCEL_OPTION) {
        System.out.println("No file is selected");
    } else if (values == JFileChooser.ERROR_OPTION) {
        System.out.println("Error!");
    } else if (file == null) {
        System.out.println("No File is chosen");
    }

I hope you guys can help. I hope my question is clear because I have problems with explanations and with the English language.

1 Answers
Related