File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JOptionPane.showConfirmDialog question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JOptionPane.showConfirmDialog question" Watch "JOptionPane.showConfirmDialog question" New topic
Author

JOptionPane.showConfirmDialog question

Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
Look at the following code. When run, expected behavior is when "Yes" is selected, 0 returned. when "No" is selected 1 is returned. I found it pretty odd that above is true when you use mouse. It is not true when you use the keys. After you see the confirmation dialog, hit tab to move focus
to "No" and hit enter, you get output of 0. Same thing happens when you use the ALT+N combination.
Could anyone help me out this?
Thanks
Here is the code:
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, let me preampt this with I don't know why this is, but it is...
The ENTER key will push the Default button no matter what. On a JPOptionPane the default Button is the OK or YES button.
Pressing the Space Bar on the focused Button will do what you want.
Also, ALT-N worked just fine for me. I got the desired result of 1 when I did an ALT-N and ALT-Y returned 0.


GenRocket - Experts at Building Test Data
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
Depends on the look and feel--Windows look and feel does what you expect...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by maggie woo:
Depends on the look and feel--Windows look and feel does what you expect...


Good to know maggie. Thanks.
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
Thanks. Both of you.
That's very helpful.
The ENTER key will push the Default button no matter what. On a JPOptionPane the default Button is the OK or YES button.

Did not know that. Browsed the API and may have missed it.
Have a good one.
Bing
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
Originally posted by Gregg Bolinger:

Good to know maggie. Thanks.

Sure thing! But as always, the answer to the question may not be the same as the solution to the problem. We played around with making our apps use WindowsLookAndFeel, and it made the app about as unstable as any other Windows app, so we stopped using it. Perhaps they were a little too true to the look and particularly the "feel" of that PLAF.
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
That's why I endup writing my own ConfirmDialog to avoid explicitly specify any look and feel
Bing
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JOptionPane.showConfirmDialog question