Problem in setting default button
I have lot of dialogs in my application and i want to designate one button in each dialog as default button with the following behaviour:
When the dialog got focus, hitting Enter/return key should trigger action on the default button even the button doesnt have keyboard focus.
I tried with the following code:
getRootPane().setDefaultButton(buttonObj);
But this doesnt satisfy my requirement. On hitting enter key the button which has focus triggers event but not the button i desire.
Please let me know hot to achieve my requirement.
From:
http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
//Make textField get the focus whenever frame is activated.
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
textField.requestFocusInWindow();
}
});
This example is for a textfield...just apply it to your button.
Thanks for your response.,
This code snippet works well only when the dialog gets displayed.
If i click on some other button in the dialog, then focus will get transferred to those buttons. In that case on hitting Enter key the button which got focus at present gets triggered. Not the button desired by me.
Let me rephrace my requirement:
I want a button to be triggered on hitting Enter key eventhough the desired button doesnt have keyboard focus. i.e. my desired button should be triggered even if some other button has keyboard focus on hitting Enter key.
It sounds like you want the <Enter> keystroke to "only" press the default button, unless another button has focus (like the Cancel button).
To do this, you would need to add a KeyListener to the other, non-button elements (text fields, combo boxes, etc...), query for the Enter key and if pressed, take the action associated wtih the desired default button.
First, create a Key Listener inner class:
class EnterKeyListener extends KeyAdapter {
public void keyReleased(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_ENTER) {
// do my default action
MyDialog.this.doDefaultBehavior();
}
}
}
then, add it to the various visual components where focus could occur:
...
textField.addKeyListener(new EnterKeyListener());
comboBox.addKeyListener(new EnterKeyListener());
....
and that should do it.
Actually, I think the request is for the <Enter> press to activate the default button even if another button has focus. The above solution seems like it should work but should only need to be added to the other components that accept the <Enter> keypress (all other button and textareas, etc).
I would recommend making a subclass of JDialog that will take care of this behavior and then make all of your dialogs derived from that class. You could have methods like, setDefaultButton and registerAsNonDefault. The registerNon method would add the keyListener listed above which would reference the default button sent to defaultButton.
Message was edited by:
BaltimoreJohn
FYI: If my above post does accurately describe what you are trying to do, you might REALLY want to rethink that and make sure that is what you want to do. You will be going against years of GUI standards and usage and (I think) confusing your users.
The standard for almost all GUIs is that if the Enter key is pressed while a button has focus, that button is selected. Most people are used to that behavior and expect it. By changing this behavior you will be giving a user an interface that goes against what he / she expects normal GUI behavior to be. In general, this is not good design because if requires the user to have application specific information (read "Design of EveryDay Things"). In it you will find arguments for the statement "If there is a standard, use it".