捕获DefaultCellEditor组件的关键事件

时间:2016-04-09 19:49:19

标签: java swing jtable keyboard-events tablecelleditor

当它写入JTable的单元格时,没有此单元格处于编辑状态。即,当它以蓝色背景颜色显示时。单元格将进入编辑模式,文本写入将显示在编辑器组件的当前文本末尾(TextField)。

但是,在此状态下TextField的{​​{1}}仍然没有焦点。而关键事件并不属于文本领域。

这些关键事件的接收者是什么组件? 我如何捕获这些关键事件?

1 个答案:

答案 0 :(得分:0)

The component that receive the events before that EditorComponent get the focus is the JTable itself.

The JTable passes the key events to the editor component of the selected cell via key binding through the processKeyBinding method. For this reason no key event listeners are notified

My problem was that my custom editor had not the processKeyBinding because it was a JPanel (composed editor). Then the Key events was lost.

The solution is pass the processKeyBinding action using a custom TextField with a puclic function to do this feature.

public class KeyBindingTextField extends JTextField {
    protected boolean processKeyBindingPublic(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
        return super.processKeyBinding(ks, e, condition, pressed);
    }  
}

Then, I pass the key binding method to TextField from JPanel so:

  protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
     return TextField.processKeyBindingPublic(ks, e, condition, pressed);
  }