Can JTable cell edit consume key strokes?

自闭症网瘾萝莉.ら 提交于 2019-12-04 22:17:37

It's a bug (which I thought had been fixed ages ago ... tsssee) due to a rather weird key processing of JTable. It starts editing in processKeyBinding on a pressed keyEvent - if autoStartEdits is true, as it is by default - and then passes that key on to the editingComponent. So at the end of the day, the key is consumed if the editingComponent consumes it. TextComponents consume a key on typed, not pressed ... which allows the pressed to travel up the dispatch chain until it reaches the menu.

A hackaround (beware: all hacks are dirty and might have unforeseen/unforseable side-effects!) is to override the table's processKeyBinding and consume the key if it started an edit:

@Override
protected boolean processKeyBinding(KeyStroke ks,
        KeyEvent e, int condition, boolean pressed) {
    boolean result = super.processKeyBinding(ks, e, condition, pressed);
    if (isEditing() && pressed) return true;
    return result;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!