2

Is it possible to call a confirmation dialog, that would have ONLY YES and NO options (without the CANCEL option)?

JOptionPane.showConfirmDialog(null, "Are you sure?")

Gives three options, but I need only two.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Buras
  • 3,069
  • 28
  • 79
  • 126
  • Not a dupe -- that question is about setting the options arbitrarily, which would be overkill here. – Etaoin Mar 19 '13 at 05:56

4 Answers4

5

Yes. it is possible.

int result = JOptionPane.showConfirmDialog(null, 
   "Are you sure you wish to exit application?",null, JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION) {
    System.exit(0);
} 
Bhavesh Shah
  • 3,299
  • 11
  • 49
  • 73
  • 2
    Rather then using a "magic number" could use the constant `JOptionPane.YES_NO_OPTION` instead. Not only is it more readable, if, for some reason, the constant changes, it won't break your code – MadProgrammer Mar 19 '13 at 05:45
3

Try using the other Overload method of JOption.showConfirmDialog method. that takes optionType. You can pass YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTION option types.

JOptionPane.showConfirmDialog(null, "Are you sure?", "Message",
        JOptionPane.YES_NO_OPTION);
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
1

You can use the other showConfirmDialog where you can specify the optionType.

E.G.

JOptionPane.showConfirmDialog(null, "Test", "Test1", JOptionPane.YES_NO_OPTION);

From the docs:-

Brings up a dialog where the number of choices is determined by the optionType parameter.

Community
  • 1
  • 1
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • 1
    No, providing 2 will display OK_CANCEL_OPTION. You should be using 0 or more better use the constants provided in same class. – Harry Joy Mar 19 '13 at 05:22
  • Rather then using a "magic number" could use the constant `JOptionPane.YES_NO_OPTION` instead. Not only is it more readable, if, for some reason, the constant changes, it won't break your code – MadProgrammer Mar 19 '13 at 05:22
0

You can use: int answer = JOptionPane.showConfirmDialog(null, "Are you sure question?", "titleToYouMessageBox", JOptionPane.YES_NO_OPTION);

CHEBURASHKA
  • 1,623
  • 11
  • 53
  • 85