Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Pressing the "Enter" key for JOptionPane.showOptionDialog

843806Oct 3 2008 — edited Jul 3 2009
I am using JOptionPane.showOptionDialog with the default "Yes", "No", and "Cancel" options.

When I press "Enter", the "Yes" option is activated, and when I press the "Tab" twice, it first selects the "No" button and then the "Cancel" button. However, even when the "No" or "Cancel" button is selected, pressing the "Enter" key still activates the "Yes" option, and not the "No", or "Cancel" cancel option.

I have 2 questions:
1) How can I correct this so that pressing the "Enter" key activates the selected option?
2) How can I find the "Yes", "No", and "Cancel" buttons in the JOptionPane class so that I can set the mnemonics?

Here is my code:
String message = "Do you want to save the Ananya Curves file before closing?";
int answer = JOptionPane.showOptionDialog(AnanyaCurves.this, message, "Close Ananya Curves",
  JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
if (answer == JOptionPane.CLOSED_OPTION)
{
  return;
}
else if (answer == JOptionPane.CANCEL_OPTION)
{
  return;
}
else if (answer == JOptionPane.NO_OPTION)
{
  //user wants to exit without saving
}
else if (answer == JOptionPane.YES_OPTION)
{
  save();
}
//code for exit
Thanks for your time looking at this!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 31 2009
Added on Oct 3 2008
9 comments
1,906 views