>To elaborate, I have subclassed both JTextField and JDialog. In
>JDialog, I have code in the JDialog subclass to click the cancel button
[quoted text clipped - 5 lines]
> KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ),
> JComponent.WHEN_IN_FOCUSED_WINDOW );
You have an action that clicks a button? That's very, um, creative!
Here's how I do it. To have the ESC key close the dialog, I use this
method:
protected void dialogInit()
{
super.dialogInit();
JLayeredPane layeredPane = getLayeredPane();
closeDialogAction = getCloseDialogAction();
String actionName = "close-dialog";
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
layeredPane.getActionMap().put(actionName, closeDialogAction);
layeredPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(stroke, actionName);
}
When I create the "Cancel" button, I attach the same action to it:
cancelButton = new JButton(dialog.getCloseDialogAction());
In my case, the button's label is taken from the action's properties,
but you can also set it yourself:
cancelButton.setText("Cancel");
As for the ENTER key, all you should have to do is set the
corresponding button as the RootPane:
dialog.getRootPane().setDefaultButton(okButton);
However, if you add an ActionListener to your JTextField, it will
consume the ENTER key, as that's what triggers the ActionLisetner.
Otherwise, as Jim said, JTextField will ignore the ENTER key.
Jason Siemens - 15 Jan 2004 15:58 GMT
Thanks Alan,
I found out what my problem was...my own stupidity. Last week, I
changed my subclass of JTextField to inherit from JFormattedTextField
instead and completely forgot I did it. Of course, this is a completely
different component and this problem is a known bug in 1.4.1 (Bug parade
#4741926).
So, I got around it by adding an action handler (for both Enter and
Escape) to my dialog like this :
JRootPane rootPane = getRootPane( );
KeyStroke ky = KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 );
rootPane.getActionMap( )
.put( InterfaceConstants.DEFAULT_BUTTON_ACTION_NAME,
new ButtonClickAction( defaultButton ) );
rootPane.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW )
.put( ky, InterfaceConstants.DEFAULT_BUTTON_ACTION_NAME );
And then adding a KeyListener to the formatted text field that calls the
action attached to the root pane directly:
public void keyPressed( KeyEvent e ) {
int key = e.getKeyCode( );
if ( key == KeyEvent.VK_ESCAPE ) {
/* call the cancel button action */
Action cancelAction = getRootPane( )
.getActionMap( )
.get(
InterfaceConstants.CANCEL_BUTTON_ACTION_NAME );
if ( cancelAction != null ) {
cancelAction.actionPerformed( null );
}
} else if ( key == KeyEvent.VK_ENTER ) {
/* call the default button action */
Action defaultAction = getRootPane( )
.getActionMap( )
.get(
InterfaceConstants.DEFAULT_BUTTON_ACTION_NAME );
if ( defaultAction != null ) {
defaultAction.actionPerformed( null );
}
}
}
Thanks for your help.
>>To elaborate, I have subclassed both JTextField and JDialog. In
>>JDialog, I have code in the JDialog subclass to click the cancel button
[quoted text clipped - 40 lines]
> consume the ENTER key, as that's what triggers the ActionLisetner.
> Otherwise, as Jim said, JTextField will ignore the ENTER key.