In every window/panel I have the Enter key mapped as the defaultButton (meaning that pressing Enter triggers a button press even though the pressed button isn’t in focus). What I need to do is map the Escape key to another button which also triggers the second button, regardless of focus. The code for the enter key is:
// We get the button
IButton iButton = (IButton)actionToolbar.getComponent(actionToolbar.getComponentCount() - 1);
// We get the window
Window window = SwingUtilities.getWindowAncestor(this);
// Only these two types of windows should have this behaviour
if (window instanceof JFrame) {
((JFrame)window).getRootPane().setDefaultButton(iButton);
}
else if (window instanceof JDialog) {
((JDialog)window).getRootPane().setDefaultButton(iButton);
}
Now what I need is basically the same code but with changing Enter with Escape, or adding a listener to…. something, I’m unsure what.
EDIT: I’m have to do this in Java 1.4 which I know it would be great if I said it immediately.
I managed to solve this by adding a key listener to the opened panel, like this