简体   繁体   English

对单个列禁用JTable的默认键绑定

[英]Disable default keybinding of JTable for a single column

I'm using autocomplete in my JTable column which uses the up and down key to move so as the JTable how can I disable up and down keybinding of JTable just for that column and again resume its functionality for next columns. 我用我的自动完成JTable ,它使用列, 键为所谓移动JTable我如何可以禁用向上和向下的按键绑定JTable只是该列,并再次恢复其功能为下一列。

table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
     .put(KeyStroke.g   etKeyStroke(KeyEvent.VK_UP, 0), "UP");
table.getActionMap()
     .put("UP", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
        //do something on JTable enter pressed
        }
      });

now I have use this code in if codition to disable the up key but as soon as the control comes out of if conditon i want the default behaviour of up key which is not happning 现在,我已经在if代码中使用了此代码,以禁用向上键,但是一旦条件消失,只要控件退出,我就希望默认键的默认行为不会发生

Column and row sensitivity is a tricky thing with JTables; 使用JTables时,列和行的灵敏度是一件棘手的事情。 but it is queryable. 但它是可查询的。 TableColumn objects do not extend Component , so you cannot simply add an action listener to them. TableColumn对象不会扩展Component ,因此您不能简单地向它们添加操作侦听器。 However, just adding this once: 但是,只需添加一次即可:

new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        //Following line is untested code, but you get the idea:
        if(!Arrays.asList(table.getSelectedColumns()).contains([your column number]) {
            //perform up action
        }
    }
}

should do the job, and it's probably more efficient than adding and removing an anonymous class to your JTable. 应该可以完成这项工作,它可能比在JTable中添加和删除匿名类更为有效。 (To Swing/AWT, columns all look the same.) (对于Swing / AWT,列的外观都相同。)

Again I apologize for not testing this code personally, I'm on my way out the door for a two day trip. 我再次为未亲自测试此代码而感到抱歉,我正准备进行为期两天的旅行。 My core point is that your best option is to query the column in the ActionListener after the method is called, with a simple boolean test. 我的核心观点是,最好的选择是在调用该方法后使用简单的布尔测试来查询ActionListener的列。

I'm using autocomplete in my JTable column 我在JTable列中使用了自动完成功能

This implies that some component other that JTable has focus. 这意味着JTable还关注其他组件。 Therefore you would add your custom Key Bindings to that component, not the JTable. 因此,您应该将自定义键绑定添加到该组件,而不是JTable。 Then those Key Bindings will have priority over the key bindings in the table. 然后,这些键绑定将优先于表中的键绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM