4

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);
                }
              }
Naame Nameee
  • 63
  • 1
  • 3
  • 8
  • for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, otherwise answers to your very vague question couldn't ...., be sure that before to read [Oracle tutorial How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – mKorbel Apr 06 '13 at 16:26

6 Answers6

22

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.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
3
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!

gus
  • 31
  • 1
  • but remove(dialogButton); is wrong – gus Jun 28 '15 at 07:01
  • 1
    Welcome to StackOverflow! You can [edit] your posts and delete your comments. Please go ahead and add your comment to your answer. While you're at it, you should describe why you think this is such a good solution and why `remove()` is wrong. – Artjom B. Jun 28 '15 at 18:32
1

Change the code to

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

Extreme Coders
  • 3,441
  • 2
  • 39
  • 55
0
if(dialogButton == JOptionPane.YES_OPTION) { // <<< start
    System.exit(0);
        if(dialogButton == JOptionPane.NO_OPTION) {
            remove(dialogButton);
        }
}// <<< stop

The result is caused by the fact that the outer if encloses the other if statement, make sure you don't next the if statement, it should be as follows : -

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

Another thing is this line int dialogButton = JOptionPane.YES_NO_OPTION;, change it to

int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
tmwanik
  • 1,643
  • 14
  • 20
0

If you want the JOptionPane to be gone then you can:

optionPane.setVisible(false);

If you don't then look at a different answer.

Supermath101
  • 417
  • 3
  • 10
-1
if (JOptionPane.showConfirmDialog(this, "sfd", "sd", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
    jProgressBar1.setValue(jProgressBar1.getValue() + 10);
    jProgressBar1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
else {
    System.exit(0);
}
Sergey Weiss
  • 5,944
  • 8
  • 31
  • 40