Dej*_*kic 3 java swing key-bindings jcombobox
我试图找出如何检测当用户按Tab键或通过鼠标单击组件区域外时JComboBox是否失去焦点.
将FocusListener添加到JComboBox的编辑器组件对我没有帮助,因为我无法确定用户是否使用了鼠标或通过tab键移动了焦点.任何想法将不胜感激.
编辑1:我想要实现的是:
编辑2:似乎我必须使用setFocusTraversalKeysEnabled(false)
在TAB被按下时获得通知,当我捕获该事件时,我应该手动转移焦点...我不喜欢这个解决方案,但这是迄今为止我能来的最好的起来.
解:
以下Java代码实际上解决了我的问题.正如我在Edit 2中所写,最简单的解决方案是禁用焦点遍历.我无耻地借用了Kleopatra的代码,现在一切正常.:)
if (!isTableCellEditor()) {
comboBoxEditor.setFocusTraversalKeysEnabled(false);
Action myAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
handleTabPress();
comboBoxEditor.transferFocus();
} // actionPerformed() method
};
comboBoxEditor.getActionMap().put("tab-action", myAction);
comboBoxEditor.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke("TAB"), "tab-action");
} // if
Run Code Online (Sandbox Code Playgroud)
感谢所有参与者的讨论!
据我了解你的问题,有两个不同的问题
如果是这样,答案是
在代码中:
final JComboBox simpleBox = new JComboBox(Locale.getAvailableLocales());
// this line configures the combo to only commit on ENTER
// or selecting an item from the list
simpleBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
//
// simpleBox.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
// Collections.EMPTY_SET);
// just noticed the OPs edit - following indeed is easier to disable _all_ traversal
// keys with one statement
simpleBox.setFocusTraversalKeysEnabled(false);
Action myAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
LOG.info("got it!");
simpleBox.transferFocus();
}
};
simpleBox.getActionMap().put("tab-action", myAction);
simpleBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke("TAB"), "tab-action");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3408 次 |
最近记录: |