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
  • 166,747
  • 40
  • 210
  • 420
Buras
  • 2,899
  • 27
  • 78
  • 122
  • 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,099
  • 8
  • 48
  • 70
  • 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,469
  • 2
  • 26
  • 40
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
  • 43,125
  • 11
  • 82
  • 101
  • 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,533
  • 9
  • 50
  • 84