简体   繁体   中英

How to call JOptionPane with only Yes/No options?

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.

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);
} 

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);

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

EG

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.

你可以使用:int answer = JOptionPane.showConfirmDialog(null,“你确定问题吗?”,“titleToYouMessageBox”,JOptionPane.YES_NO_OPTION);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM