组合框弹出并使用键盘快捷键选择 [英] Combo Box pop up and select using keyboard shortcuts

查看:144
本文介绍了组合框弹出并使用键盘快捷键选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   public static void comboBoxActionPerform(JComboBox  comboBox)
    {
        String ACTION_KEY = "myAction";

        Action actionListener = new AbstractAction()
        {   
            @Override
            public void actionPerformed(ActionEvent actionEvent)
            {
                JComboBox source = (JComboBox) actionEvent.getSource();
                source.showPopup();
                source.setFocusable(true);
            }
        };

        KeyStroke ctrlT = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
        InputMap inputMap = comboBox.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(ctrlT, ACTION_KEY);
        ActionMap actionMap = comboBox.getActionMap();
        actionMap.put(ACTION_KEY, actionListener);
        locationTypeComboBox.setActionMap(actionMap);
    }

我有一个com框,调用上述方法可以弹出组合框按下(Ctrl + L)键。
弹出组合框。但是我无法使用UP / DOWN键选择其中的项目。
当我按Ctrl + L时,组合框没有对准焦点。那可能是要解决的问题。
如果我手动选择组合框,则向上/向下工作正常。需要您的帮助。

I have a com box and I call the above method to pop up combo box on keys (Ctrl+L) pressed. It pops up the combo box. But I can't select the items in it using UP/DOWN keys. Combo box get not focused when I pressed Ctrl+L. That might be the issue to be fixed. If I select the combo box manually and then up/down works fine. Need your help.

推荐答案

您要查找的方法是requestFocus,而不是setFocusable

The method you are looking for is requestFocus, not setFocusable

    Action actionListener = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            JComboBox source = (JComboBox) actionEvent.getSource();
            source.requestFocus();
            source.showPopup();
            // source.setFocusable(true);
        }
    };

顺便说一句,重置组件的actionMap并不常见。

BTW, it's unusual to reset the actionMap of a component.

这篇关于组合框弹出并使用键盘快捷键选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆