dimanche 27 novembre 2016

Exiting loop when user clicks "cancel" or finishes number of allowed attempts

I want to code a loop that ends when the user has used up their three tries, clicks cancel or closes the window with the red x. The red x and using up the three tries portion work fine. But the cancel button is being useless and acting the same as the OK button.

I have written an if statement but it is ineffective. The window that prompts the user for a password continues to pop up until all allowed attempts are used up even when the user presses cancel. This is what I have so far:

    String[] options = new String[]{"OK", "Cancel"};

    int tries = 0;

    String passString = "";

    int option = JOptionPane.OK_OPTION;

    while (!isValidPassword(passString) && tries < 3) {

            option = JOptionPane.showOptionDialog(null, panel, "The title",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options, options[1]);


            if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
                break;
            }

Aucun commentaire:

Enregistrer un commentaire