vendredi 30 octobre 2015

Java: Bug: JFileChooser only showing up sometimes

I have a method to open JFileChooser and allow to user to choose a file to later copy the URL of it. However, JFileChooser only shows up sometimes I run the program (?).

I do not understand what is wrong with the code. Thanks in advance.

More clearly: The UI of the JFileChooser does not show up.

    String readThisGlos = null;

    JFrame parentFrame = new JFrame();
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Choose");
    int userSelection = fileChooser.showOpenDialog(parentFrame);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = fileChooser.getSelectedFile();

        readThisGlos = fileToSave.getAbsolutePath();
    } else {
        // Error
    }
    System.out.println(readThisGlos);
    return readThisGlos;

Edit: I know the method is running, because when I put a System.out.println("//something"); in the beginning of the method, it works.

If I put System.out.println("//something"); between fileChooser and int userSelection, the UI shows up.

Aucun commentaire:

Enregistrer un commentaire