wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Dialog box help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dialog box help" Watch "Dialog box help" New topic
Author

Dialog box help

Angie Lathum
Greenhorn

Joined: Feb 22, 2001
Posts: 6
I am trying to make a dialog box with Yes and No buttons that has the No button as the default, but I keep getting Yes as the default. I spent about 2 hours yesterday trying to figure out the problem and its making me crazy - can anyone tell me whats wrong?
Here is the code I'm using (message and title are previously defined strings):
JButton[] choices = new JButton[2];
choices[0] = new JButton("Yes");
choices[1] = new JButton("No");
int response = JOptionPane.showOptionDialog( this,
message,
title,
0,
JOptionPane.WARNING_MESSAGE,
null,
choices,
choices[1]);

[This message has been edited by Angie Lathum (edited March 08, 2001).]
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
I run your code
It does take the 'No' button as default but the focus is on 'Yes' button.
When this dialog pops up if you press enter key, you see that 'No' button being pressed not 'Yes' button.
Do you want the focus to be on 'No' button?
Cheers
Siva Prasad
Angie Lathum
Greenhorn

Joined: Feb 22, 2001
Posts: 6
Yes, I would like the No button to have the focus. How do I do that?
I feel like a goof, I never actually tried pressing Enter - I just looked at it and thought Yes was the default. Sigh.
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Angie
Before I answer your question may I know why you need the focus on 'No' button?
the basic behaviour of default and focus is like this
default - it takes the action from 'Enter' key from the keyboard
focus - it takes the action from 'spacebar' key from the keyboard.
I guess when coming to these dialog, it always depends on the 'default' thing because the user always presses 'enter' key
Siva Prasad
Angie Lathum
Greenhorn

Joined: Feb 22, 2001
Posts: 6
I didn't realize that is how the default and focus worked. I always thought that the button with the dashed lines around the text is the button that would be pressed when Enter key is pressed.
I'm understanding you to say that the button with the dashed lines is the one with the focus and will be activated by the spacebar. Is that right?
Anyway... the dialog box I'm trying to make will be shown to confirm that user wants to do an irreversible delete. I am trying to avoid possibility where user does not want to delete data, but accidentally presses a key and data is deleted.
Maybe I'm being to cautious and getting away from normal dialog box behaviour. What do you think?
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Originally posted by Angie Lathum:
I'm understanding you to say that the button with the dashed lines is the one with the focus and will be activated by the spacebar. Is that right?

Yes you are right Angie.
So if you want get the irreversable effect just modify the code in such a way that you pass the 'No' option as default like this...
if your choices[] array contains choices like this {"No","Yes"}
pass choices[0] as the last argument in the following function call.
int response = JOptionPane.showOptionDialog( this,
message,
title,
0,
JOptionPane.WARNING_MESSAGE,
null,
choices,
choices[0]);
Hope this helps
Siva Prasad
 
 
subject: Dialog box help