JOptionPane YES NO OPTION

100,865

Solution 1

You should actually take the result from the option pane:

dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);

Otherwise, it remains set to JOptionPane.YES_NO_OPTION.

Cleaner would be:

if (JOptionPane.showConfirmDialog(null, "Are you sure?", "WARNING",
        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
    // yes option
} else {
    // no option
}

Although, I'm not sure what this line is expected to do in the posted code: remove(dialogButton);.

For more details and examples check out How to Make Dialogs tutorial.

Solution 2

int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING",JOptionPane.YES_NO_OPTION);

if(dialogButton == JOptionPane.YES_OPTION) {
System.exit(0);}else {remove(dialogButton);}

this is the correct!

Solution 3

Change the code to

int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);

Share:
100,865
Naame Nameee
Author by

Naame Nameee

Updated on February 10, 2020

Comments

  • Naame Nameee
    Naame Nameee about 4 years

    I got an JOptionPane and yes and no buttons. But, whichever button you click it still exists. HELP! Heres the code:

    int dialogButton = JOptionPane.YES_NO_OPTION;
                JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
                if(dialogButton == JOptionPane.YES_OPTION) {
                    System.exit(0);
                if(dialogButton == JOptionPane.NO_OPTION) {
                      remove(dialogButton);
                    }
                  }