如何让JComboBox响应输入密钥

时间:2015-08-04 18:52:15

标签: java swing events jcombobox keylistener

我现在已经把我的大脑震撼了一段时间。当我运行以下代码时,任何其他键在调用JComboBox的showPopup()方法时都能正常工作,但只要按下回车键,就不会发生任何事情。我试图触发鼠标事件来模拟用户物理点击JComboBox,但到目前为止还没有任何工作。 (我可以使用java.awt.Robot,但我真的不愿意。)。这是一个示例程序,它只显示一个JComboBox,并向其添加一个KeyAdapter:

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SpringLayout;

public class Test {

    public static void main(String[] args) {
        JFrame testFrame = new JFrame();
        testFrame.setLocationRelativeTo(null);
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SpringLayout layout = new SpringLayout();
        testFrame.getContentPane().setLayout(layout);

        JComboBox testingComboBox = new JComboBox(new String[] {"Option 1", "Option 2", "Option 3"});
        testingComboBox.addKeyListener(new KeyAdapter(){

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER){
                    testingComboBox.showPopup();
                } else if (e.getKeyCode() == KeyEvent.VK_2){
                    testingComboBox.showPopup();
                }
            }

        });

        testFrame.add(testingComboBox);
        layout.putConstraint(SpringLayout.NORTH, testingComboBox, 0, SpringLayout.NORTH, testFrame);
        layout.putConstraint(SpringLayout.WEST, testingComboBox, 0, SpringLayout.WEST, testFrame);
        testFrame.pack();
        testingComboBox.requestFocusInWindow();
        int differenceInWidth = testFrame.getWidth() - testFrame.getContentPane().getWidth();
        int differenceInHeight = testFrame.getHeight() - testFrame.getContentPane().getHeight();
        testFrame.setMinimumSize(new Dimension(testingComboBox.getWidth() + differenceInWidth, testingComboBox.getHeight() + differenceInHeight));
        testFrame.setVisible(true);
    }

}

我不太确定发生了什么,并希望得到任何可能的帮助。

注意:我也尝试过使用ActionListener,这也会产生同样的问题。如果我在showPopup()调用之前放置System.out.println("Test");,"测试"仍然在命令行中打印,但没有任何内容出现。

2 个答案:

答案 0 :(得分:2)

不要使用KeyListener。请改用键绑定。这应该有效:

testingComboBox.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
                "showPopup");
testingComboBox.getActionMap().put("showPopup",
                new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        testingComboBox.showPopup();
                    }
});

答案 1 :(得分:2)

Swing旨在与http://example.domain.com/appname/health一起使用。

Enter键已被定义为JComboBox的绑定,因此该操作基本上会导致弹出窗口关闭。这可以通过使用:

来证明
SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        testingComboBox.showPopup();
    }
});

通过使用invokeLater(),代码放在EDT的末尾,以便在默认的Enter键处理之后执行。

所以实际上你不应该试图听取Enter键事件,因为它们已经被UI处理了。

查看Key Bindings以获取每个组件的默认键绑定列表。

当你查看列表时,你会注意到一个组合框已经有一个Action来切换弹出窗口,所以如果你创建了一个Key Binding,你应该使用现有的Action。以上链接显示了如何执行此操作:

KeyStroke ks = KeyStroke.getKeyStroke("2");
InputMap im = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(ks, "togglePopup");