【发布时间】:2011-02-11 23:08:43
【问题描述】:
我尝试在 JComboBox 上使用 getInputMap() + getActionMap() 似乎没有效果。
我已经在 JComboBox 上尝试了 addActionListener() / addItemListener(),但我似乎无法区分选择更改与按下 Return/Enter 键的人。
有什么建议吗?在我的应用程序中,我希望 Return/Enter 键比选择更强大,它是一个选择 + 应用操作。
这是我设置键绑定的代码。当组件是JList 时它可以正常工作(例如调用note("hit ENTER")),但当组件是JComboBox 时它不起作用。
private void setupApplyProfile(final JComponent component, final MyComboBoxModel mcbm)
{
String enterAction = "applyItem";
KeyStroke enterKey = KeyStroke.getKeyStroke("ENTER");
component.getInputMap().put(enterKey, enterAction);
component.getActionMap().put(enterAction, new AbstractAction()
{
@Override public void actionPerformed(ActionEvent e) {
note("hit ENTER");
applySelectedProfile(mcbm);
}
});
}
【问题讨论】:
-
你是如何使用 getActionMap() 的?