Article View: borland.public.jbuilder.java.swing
Article #10740Re: Focus traversal on Enter key?
From: wim.van.leuven
Date: Tue, 17 Aug 1999 00:00
Date: Tue, 17 Aug 1999 00:00
88 lines
3613 bytes
3613 bytes
The indicated problem of the JTextField component is that it descends
from JTextComponent wich by default handles the <Enter> key. In the
documentation of JTextField is described how one can make a JTextField
not to behave like an AWT TextField, but rather to activate the default
button on <Enter>:
static
{
JTextField f = new JTextField();
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
Keymap map = f.getKeymap();
map.removeKeyStrokeBinding(enter);
}
Notice that this is static code and result in the fact that every
JTextField, instantiated from then on, will no longer react to the
<Enter> key, but rather pass it on to its enclosing container, and so
on. If this one of this enclosing containers is a JFrame with a default
button, this frame will map the <Enter> to the activation of this
button.
So in the same way, one can easely add an action listener to do a next
focus on <Enter> for all JTextFields.
Hope this helped.
Bye,
Wim Van Leuven.
In article <7otutj$m95$1@nnrp1.deja.com>,
Christophe Caenevet <christophe_caenevet@bigfoot.com> wrote:
> I've got a method to do it for all the components you want :
>
> I make the folllowing class :
>
> public class EnterKeyAdapter extends KeyAdapter {
> Window frame ;
>
> public EnterKeyAdapter ( Window _frame ){
> frame = _frame;
> }
>
> public void keyPressed(KeyEvent e) {
> if (e.getKeyCode()==KeyEvent.VK_ENTER){
> FocusManager fm = FocusManager.getCurrentManager();
> if (fm instanceof DefaultFocusManager)
> fm.focusNextComponent(frame.getFocusOwner());
> }
> }
> }
>
> to put this adapter I do the following code during the windowOpened
> event :
> (the pane to start this function is the result of GetContentPane())
>
> private void addEnterKeyPressedEventListener( Container pane){
> Component currentComponent ;
> int count = pane.getComponentCount();
> for (int index=0; index < count ; index ++ ){
> currentComponent = pane.getComponent(index);
> if ( !(currentComponent instanceof JPanel)
> && !(currentComponent instanceof JButton)
> && !(currentComponent instanceof JLabel)
> )
> currentComponent.addKeyListener(new EnterKeyAdapter(this));
> if ( currentComponent instanceof JPanel)
> addEnterKeyPressedEventListener((Container)currentComponent);
> }
> }
>
> --
> ===============================================
> Professionnal : christophe_caenevet@bigfoot.com
> Personnal : caenevet_xelot@bigfoot.com
> ===============================================
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
>
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Technical Details
Message-ID:
<7pbf8s$o6b$1@nnrp1.deja.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7opftu$aal4@forums.borland.com> <7opra1$al11@forums.borland.com> <7otutj$m95$1@nnrp1.deja.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,9fed77d5e06193b9 X-Google-Attributes: gid111d45,public From: wim.van.leuven@watco.be Subject: Re: Focus traversal on Enter key? Date: 1999/08/17 Message-ID: <7pbf8s$o6b$1@nnrp1.deja.com> X-Deja-AN: 513733672 References: <7opftu$aal4@forums.borland.com> <7opra1$al11@forums.borland.com> <7otutj$m95$1@nnrp1.deja.com> X-Http-Proxy: 1.0 x31.deja.com:80 (Squid/1.1.22) for client 193.74.148.90 Organization: Deja.com - Share what you know. Learn what you don't. X-Article-Creation-Date: Tue Aug 17 10:58:38 1999 GMT X-MyDeja-Info: XMYDJUIDvlv Newsgroups: borland.public.jbuilder.java.swing X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)