JComboBox 第二个 KeyListener 防止第一个被解雇
JComboBox second KeyListener prevents first to get fired
我观察到以下行为:
向 JComboBox 添加一个新的 KeyListener 并调用 e.consume() 阻止默认 KeyListener 触发其事件。
这是我的示例代码:
final JComboBox cb = new JComboBox(fooList);
final KeyListener fooKeyListener = new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("VK_DOWN fired");
e.consume();
}
}
};
cb.addKeyListener(fooKeyListener);
contentPanel.add(cb);
在此之后,当我按下向下箭头按钮时,我只会输出 "VK_DOWN fired" 并且整个 Scrolling/Selecting-Mechanism 停止 工作。
我希望,负责所有滚动内容的默认 KeyListener 首先执行,因此 e.consume() 不应阻止它工作。顺便说一下,调用 e.consume()
是 唯一 停止滚动机制的方法!即使调用 cb.removeKeyListener(cb.getKeyListeners()[0]);
也不会 停止滚动机制的工作(在我看来,这似乎很奇怪!)。
感谢任何帮助!
I would expect, that the default KeyListener, which is responsible for all that scrolling-Stuff gets executed first,
实际上事件触发的顺序并没有定义,你永远不应该假设一个特定的顺序。事实上,当前的默认实现调用最先添加的最后一个侦听器。
Even calling cb.removeKeyListener(cb.getKeyListeners()[0]); does not stop the Scrolling mechanism from working (which, in my opinion, seems to be very weird!).
Swing 旨在使用 Key Bindings. Scrolling is enabled by a default Action being mapped to the Down key. You can see all the default mappings by checking out Key Binding Defaults
calling e.consume() is the only way to stop the Scrolling mechanism!
您可以禁用向下箭头的默认值 Key Binding
,方法是:
InputMap im = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke("DOWN"), "none");
我观察到以下行为:
向 JComboBox 添加一个新的 KeyListener 并调用 e.consume() 阻止默认 KeyListener 触发其事件。
这是我的示例代码:
final JComboBox cb = new JComboBox(fooList);
final KeyListener fooKeyListener = new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("VK_DOWN fired");
e.consume();
}
}
};
cb.addKeyListener(fooKeyListener);
contentPanel.add(cb);
在此之后,当我按下向下箭头按钮时,我只会输出 "VK_DOWN fired" 并且整个 Scrolling/Selecting-Mechanism 停止 工作。
我希望,负责所有滚动内容的默认 KeyListener 首先执行,因此 e.consume() 不应阻止它工作。顺便说一下,调用 e.consume()
是 唯一 停止滚动机制的方法!即使调用 cb.removeKeyListener(cb.getKeyListeners()[0]);
也不会 停止滚动机制的工作(在我看来,这似乎很奇怪!)。
感谢任何帮助!
I would expect, that the default KeyListener, which is responsible for all that scrolling-Stuff gets executed first,
实际上事件触发的顺序并没有定义,你永远不应该假设一个特定的顺序。事实上,当前的默认实现调用最先添加的最后一个侦听器。
Even calling cb.removeKeyListener(cb.getKeyListeners()[0]); does not stop the Scrolling mechanism from working (which, in my opinion, seems to be very weird!).
Swing 旨在使用 Key Bindings. Scrolling is enabled by a default Action being mapped to the Down key. You can see all the default mappings by checking out Key Binding Defaults
calling e.consume() is the only way to stop the Scrolling mechanism!
您可以禁用向下箭头的默认值 Key Binding
,方法是:
InputMap im = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke("DOWN"), "none");