When creating a Yes/No JOptionPane, how do I default the value to No instead of Yes?

John Zukowski

You need to explicitly pass in the button labels and initial value via the showOptionDialog() method (or constructor).

int selection = JOptionPane.showOptionDialog(CreditDefaultLegPanel.this,
   "Are you sure you want to do something bad?,
   "Confirm Bad", JOptionPane.YES_NO_OPTION,
   JOptionPane.QUESTION_MESSAGE, null, 
   new String[] {"Yes", "No"}, "No");
if (selection == JOptionPane.YES_OPTION) {
If you do this, you no longer get the automatic localization of Yes/No labels. You'll have to deal with that yourself/separately.
About | Sitemap | Contact