How to use VK_UP or VK_DOWN to move to the previous or next Textfield?

别来无恙 提交于 2020-01-03 05:50:08

问题


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.


回答1:


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);
    }
}



回答2:


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?



来源:https://stackoverflow.com/questions/11380406/how-to-use-vk-up-or-vk-down-to-move-to-the-previous-or-next-textfield

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!