java 禁止 Enter Key 在 JTable 中向下移动一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13516730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:03:31  来源:igfitidea点击:

Disable Enter Key from moving down a row in JTable

javaswingjtablekeylistenerkey-bindings

提问by maloney

I need to override the enter key functionality on a JTable. At present the default behaviour is to move the row selection down one row when the user presses the 'Enter' key. I want to disable this and get it to do something different based on their selection. The problem is that it seems to move down before it goes into my keylistener which takes in the row selection - this therefore opens another window with the wrong row selected.

我需要覆盖 JTable 上的输入键功能。目前的默认行为是当用户按下“Enter”键时将行选择向下移动一行。我想禁用它并让它根据他们的选择做一些不同的事情。问题是它似乎在进入我的键侦听器之前向下移动,该侦听器接收行选择 - 因此这会打开另一个选择错误行的窗口。

This is my code so far...:

到目前为止,这是我的代码...:

public class MyJTable extends JTable {


   public MyJTable(){
        setRowSelectionAllowed(true);
        addListeners()
    }

    public void addListeners(){

         addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent e) {}

                @Override
                public void keyPressed(KeyEvent e) {}

                @Override
                public void keyReleased(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_ENTER) {

                        openChannel();
                    }
                }
           });
    }

    public void openChannel(){
            for (int selectedRow : getSelectedRows()){
                //Code to open channel based on row selected
            }
        }
}

回答by David Kroukamp

+1 to @Robin's answer

+1 对@Robin 的回答

Adding to my comment...

添加到我的评论...

Swing uses KeyBindingssimply replace exisitng functionality by adding a new KeyBindingto JTable(the beauty happens because of JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT):

Swing 使用KeyBindings简单地通过添加一个新的KeyBinding来替换现有的功能JTable(美的发生是因为JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT):

private void createKeybindings(JTable table) {
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
    table.getActionMap().put("Enter", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            //do something on JTable enter pressed
        }
    });
}

simply call this method and pass JTableinstance to override standard functionality of JTableENTER

只需调用此方法并传递JTable实例即可覆盖的标准功能JTableENTER

回答by Robin

This is implemented using key bindings, which is preferred over key listeners. I strongly suggest you do the same: replace your key listener by a key binding.

这是使用键绑定实现的,键绑定比键侦听器更受欢迎。我强烈建议您也这样做:用键绑定替换您的键侦听器。

The solution is replace the entry in the InputMap to point to your own Action (which you insert in the action map), or to just replace the appropriate entry in the action map.

解决方案是替换 InputMap 中的条目以指向您自己的 Action(您插入到动作映射中),或者只替换动作映射中的相应条目。

The key bindings tutorialcontains more info

键绑定教程包含更多信息