Article View: borland.public.jbuilder.java.swing
Article #9420Re: how to control focus ?
From: John P. McGrath
Date: Wed, 15 Mar 2000 00:00
Date: Wed, 15 Mar 2000 00:00
62 lines
3377 bytes
3377 bytes
On Mon, 13 Mar 2000 12:28:40 +0100, Nils-Erik Thorell wrote:
> Do you happen to know how to still keep the Ok-button sensitive
> to the 'Enter'-key, while editing a textfield? That is standard
> Windows behaviour, but seems to be hard to achieve in Java.
Yup. This is a really tough one.
The problem started with the original AWT TextField class, which would
handle the ENTER key by notifying its ActionListener, sort of like it was
a button. So when the Swing JTextField class was written, in order to
make the transition to Swing easier, they implemented the same behavior.
But this interfered with code that registered a keyboard action for the
dialog, breaking "default button" logic. So the JTextField docs suggest
that you use the following code to disable the ENTER key handling.
static {
JTextField f = new JTextField();
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
Keymap map = f.getKeymap();
map.removeKeyStrokeBinding(enter);
}
What this code does is to get the single instance of Keymap that Swing
associates with JTextFields, and removes the binding for the ENTER key.
That allowed the ENTER key to propagate up the component hierarchy and be
recognized by the "default button" logic.
The danger in this is that it removes the binding for *all* JTextFields.
So if some other code (perhaps in a library) depends on this behavior,
the above code would break it.
Then in JDK 1.3, Sun unified the different key binding mechanisms in
Swing, they change this so that you could no longer remove the key
binding from the global JTextField Keymap (now an InputMap). This broke
code that depended on the hack suggested in the documentation.
Now, with JDK 1.3 RC2, they have made another change. If you have not
added an ActionListener to a JTextField, then it will propagate the ENTER
key up the component hierarchy, and the default button logic will work.
If you do add an ActionListener, then it will notify the ActionListener,
but will not propagate the ENTER.
I think this is probably the best possible solution to the problem,
given the history and the extant code that depends on old behaviors. But
how to write code that will work with all of the different JDKs is not
entirely obvious. I have found that if I remove the binding from the
global JTextField map, and then also add a binding for each individual
JTextField, it appears to work properly in JDK 1.2 and JDK 1.3 RC1.
There is a more complete discussion of the problem at:
http://developer.java.sun.com/developer/bugParade/bugs/4306756.html
I believe you will have to join the JDC (Java Developers Connection) to
see it, but that is free and very worthwhile.
--
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID:
<MPG.13391635b674d8c89897f2@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8a88dp$f6f1@bornews.borland.com> <38C847A7.988CF795@home.com> <38CCD0E8.279470DF@es.sigma.se>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,2109ba7cebc5f96e X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: how to control focus ? Date: 2000/03/15 Message-ID: <MPG.13391635b674d8c89897f2@forums.borland.com> X-Deja-AN: 597740536 References: <8a88dp$f6f1@bornews.borland.com> <38C847A7.988CF795@home.com> <38CCD0E8.279470DF@es.sigma.se> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing