Thanks for the reply.  This did not exactly help, but it was enough of a
hint that I was able to find a better solution.

First, after experimenting with other application, I decided to accept the
SPACE key action.

Second, it seems that the right way to make a button respond to the ENTER
key is to make it the default button.  If you want ALL your buttons to
respond to the ENTER key, here is a class that you can add to your JDialog
subclass:

  private FocusListener makeDefaultFocusListener = new FocusAdapter() {
    JButton defaultButton;
    public void focusGained(FocusEvent e) {
      defaultButton = getRootPane().getDefaultButton();
      getRootPane().setDefaultButton((JButton)e.getSource());
    }
    public void focusLost(FocusEvent e) {
      getRootPane().setDefaultButton(defaultButton);
    }
  };

Then just add this FocusListener to your buttons:

  Container cp = getContentPane();
  JButton jbtnLogin = new JButton("Login");
  jbtnLogin.setActionCommand("Login");
  jbtnLogin.addFocusListener(makeDefaultFocusListener);
  jbtnLogin.addActionListener(actionEventListener);
  cp.add(jbtnLogin);

If you want your dialog to have a default button you can do this as you
normally would:

  getRootPane().setDefaultButton(jbtnLogin);

For more on this see
<http://forum.java.sun.com/thread.jspa?forumID=31&threadID=244517>

Dennis

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of Joaqu�n S�nchez Jim�nez
Sent: Thursday, March 03, 2005 1:32 PM
To: Research Triangle Java User's Group mailing list.
Subject: Re: [Juglist] JButton Action Key Binding


Try dialog.getRootPane().setDefaultButton(null);

----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, March 03, 2005 7:07 PM
Subject: [Juglist] JButton Action Key Binding


> Hello to all,
>
> I been working on a little Swing application where I extends JDialog to
create a LoginDialog class.  It includes a JTextField for user id, a
JPasswordField for a password and two JButton objects -- for login and
cancel.  Upon testing this I discovered that I can tab to the JButtons, but
the ENTER key does not fire the button actions.  The button actions are
fired by the SPACE key.  I done some research using Google and found that
since Java 1.2 this is the default behavior.  I have found and successfully
tested solutions that enable the ENTER key to fire the button action.  So
far, so good.  However, I would also like to disable the default
ehavior  -- so the SPACE key does not fire the action.  Does anyone know how
to do this?
>
> Dennis
>
>
>
>
> _______________________________________________
> Juglist mailing list
> [email protected]
> http://trijug.org/mailman/listinfo/juglist_trijug.org


_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org


_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to