在 EditorComponent 获得焦点之前接收事件的组件是 JTable 本身。
JTable 通过 processKeyBinding 方法通过键绑定将键事件传递给选定单元格的编辑器组件。
因此,不会通知关键事件侦听器
我的问题是我的自定义编辑器没有 processKeyBinding,因为它是 JPanel(组合编辑器)。然后 Key 事件丢失了。
解决方案是使用带有公共函数的自定义 TextField 传递 processKeyBinding 操作来执行此功能。
public class KeyBindingTextField extends JTextField {
protected boolean processKeyBindingPublic(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
return super.processKeyBinding(ks, e, condition, pressed);
}
}
然后,我将键绑定方法从 JPanel 传递给 TextField:
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
return TextField.processKeyBindingPublic(ks, e, condition, pressed);
}