0

I want to use the VK_UP or VK_DOWN to move the focus, so it can go to the previous or next textfield.

How can i do that ?

I tried using this, but it didnt work.

 private void passwordTFKeyTyped(java.awt.event.KeyEvent evt) {      
 char c = evt.getKeyChar();

    if (c == KeyEvent.VK_UP) {
        usernameTF.grabFocus();
    }
 }

So i tried adding 'System.out.println(c)'
and the result given is empty (empty doesnt mean empty string like "" or null), its more like the UP Key isnt working.

Thank you so much.

radik
  • 3
  • 1
  • 2
  • 6

2 Answers2

2

Solution ideas

Suggesting swing maps

Depends on the widget library you use. If you are using Swing, then get the InputMap from the text field, and add suitable bindings to it. Originally I had hoped that you could copy the bindings for Tab and Shift-Tab, but as I found out in my experiments, those aren't part of the InputMap of the individual components. So you'll have to define new keys, and use the ActionMap to map those to new actions.

Problem with pasted code

The code you quoted won't work because you should use getKeyCode instead of getKeyChar. The former corresponds to those VK_ constants, whereas the latter will result in the character for a “normal” (i.e. printing) key, and only during the KEY_TYPED event. For non-printing keys, the KEY_TYPED event will never be generated, and during all other events, the key character will be CHAR_UNDEFINED instead.

Examples

These examples were added in a later edit.

I dual-license the following code: you may use it either according to the terms of CC-Wiki or the terms of the GPL version 3 or later.

Example 1: Swing input and action maps

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SO11380406a {
    static final Object focusNextKey = new Object();
    static final Object focusPrevKey = new Object();
    static final Action focusNextAction = new AbstractAction("focusNext") {
            public void actionPerformed(ActionEvent e) {
                ((Component)e.getSource()).transferFocus();
            }
        };
    static final Action focusPrevAction = new AbstractAction("focusPrev") {
            public void actionPerformed(ActionEvent e) {
                ((Component)e.getSource()).transferFocusBackward();
            }
        };
    static final KeyStroke down = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
    static final KeyStroke up = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
    private static void remap(JComponent c) {
        ActionMap am = new ActionMap();
        am.put(focusNextKey, focusNextAction);
        am.put(focusPrevKey, focusPrevAction);
        am.setParent(c.getActionMap());
        c.setActionMap(am);
        InputMap im = new InputMap();
        im.put(down, focusNextKey);
        im.put(up, focusPrevKey);
        im.setParent(c.getInputMap(JComponent.WHEN_FOCUSED));
        c.setInputMap(JComponent.WHEN_FOCUSED, im);
    }
    public static void main(String[] args) {
        JFrame frm = new JFrame("SO Question 11380406 Demo A");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.getContentPane().setLayout(new GridLayout(2, 1));
        JTextField a = new JTextField(80), b = new JTextField(80);
        frm.getContentPane().add(a);
        frm.getContentPane().add(b);
        frm.pack();
        remap(a);
        remap(b);
        frm.setLocationByPlatform(true);
        frm.setVisible(true);
    }
}

Example 2: AWT KeyListener

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SO11380406b {
    static final KeyListener arrowFocusListener = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getModifiers() == 0) {
                    if (e.getKeyCode() == KeyEvent.VK_DOWN)
                        e.getComponent().transferFocus();
                    if (e.getKeyCode() == KeyEvent.VK_UP)
                        e.getComponent().transferFocusBackward();
                }
            }
        };
    private static void remap(Component c) {
        c.addKeyListener(arrowFocusListener);
    }
    public static void main(String[] args) {
        JFrame frm = new JFrame("SO Question 11380406 Demo B");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.getContentPane().setLayout(new GridLayout(2, 1));
        JTextField a = new JTextField(80), b = new JTextField(80);
        frm.getContentPane().add(a);
        frm.getContentPane().add(b);
        frm.pack();
        remap(a);
        remap(b);
        frm.setLocationByPlatform(true);
        frm.setVisible(true);
    }
}
MvG
  • 57,380
  • 22
  • 148
  • 276
  • Ohhh i see, so we can use Shift+Tab to move the focus to the previous textfield. That should do it, haha. Thank you MvG – radik Jul 08 '12 at 08:28
  • I'm sorry for my late followup, and thank you for you advice. Yes i put it at Key_typed event, and tried to change the getKeyChar into getKeyCode. But it didnt work. Could you give me some examples code ? i dont really understand the char_undefined thing you mentioned above. Thank you so much. – radik Jul 15 '12 at 04:25
  • @radik: Added code. Noticed that the focus traversal doesn't really appear to be managed by the `InputMap` in swing. At least not for the individual components. About “the `CHAR_UNDEFINED` thing”: that simply is the constant you will obtain when you try to get the `keyChar` of a key which doesn't represent a character. – MvG Jul 15 '12 at 11:23
  • Reading the docs, I found that a non-printing key like an arrow key won't generate a `KEY_TYPED` event at all. Included that in my answer. – MvG Jul 15 '12 at 14:12
0

I'm no expert, but I don't think Java listens like that when the user is focused on a textfield. Perhaps something like DocumentListener would suit your needs?

Hellephant
  • 63
  • 1
  • 6
  • A `DocumentListener` only listens to changes to the content of a text component. As the arrow key won't change the content, the `DocumentListener` won't hear about it. – MvG Jul 10 '12 at 10:50