【问题标题】:Extend keybindings in JTable扩展 JTable 中的键绑定
【发布时间】:2017-05-05 22:33:18
【问题描述】:

F4 的绑定添加到面板内的 JTable 后,TAB 的标准行为停止工作。当前单元格进入编辑模式,而不是预期的跳转到下一个单元格。

当使用ActionMap/InputMap 或在表格中添加KeyListener 时会发生这种情况

ActionMap map = new ActionMap();
map.put("f4Action", new F4Action());
map.setParent(this.getActionMap());

InputMap input = new InputMap();
input.put(KeyStroke.getKeyStroke("F4"), "f4Action");
input.setParent(this.getInputMap());

// this is the table in question
table.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, input);
table.setActionMap(map);

或者只是 table.addKeyListener(new KeyListener() { /*...*/ } 使用其他方法。


是否可以保持标准行为并仅覆盖显式键绑定?以及如何做到这一点。

【问题讨论】:

    标签: java swing awt keylistener key-bindings


    【解决方案1】:
    ActionMap map = new ActionMap();
    

    不要创建新的ActionMap

    只需使用现有的ActionMap

    ActionMap map = table.getActionMap();
    

    请参阅Key Bindings 以获取所有当前绑定的列表以及如何自定义绑定的模板。

    【讨论】:

    • 认为覆盖特定键的解决方案在于InputMaps 的层次结构。但是您的解决方案更简单,直接......并且当然可以工作^^
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2016-02-20
    • 2020-09-08
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多