【问题标题】:Capturing Key Events of DefaultCellEditor Component捕获 DefaultCellEditor 组件的关键事件
【发布时间】:2016-07-31 00:46:05
【问题描述】:

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

但是,在这种状态下,DefaultCellEditorTextField 仍然没有焦点。并且关键事件不属于文本字段。

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

【问题讨论】:

  • 你为什么要捕捉关键事件?

标签: java swing jtable keyboard-events tablecelleditor


【解决方案1】:

在 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);
  }

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 1970-01-01
    • 2020-08-31
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    相关资源
    最近更新 更多