需要使用键盘上的箭头键来处理JTable的行

时间:2022-08-16 17:25:42

I have to shift the cells up/down depending upon the up/down arrow key pressed from the keyboard. I am adding a KeyListener (in fact KeyAdapter) on the JTable to achieve it using the keyPressed() method. Now what's happening is that when I am pressing the alphanumeric keys, then I am able to get the selected row using table.getSelectedRow(), but when I am pressing the arrow keys its giving me "-1" always. That means no row is shown selected. I also tried to set the focus on table but it did not work. The code I am usin is as follows:

我必须根据键盘上的上下箭头键上下移动单元格。我在JTable上添加了一个KeyListener(实际上是KeyAdapter),以便使用keyPressed()方法实现它。现在发生的情况是,当我按字母数字键时,我可以使用table.getSelectedRow()获取所选的行,但是当我按箭头键时,它总是给出“-1”。这意味着没有显示选定的行。我也试图把焦点放在桌子上,但没有成功。我使用的代码如下:

private void settingKeys() {
    controller.getStandardActionDetailsTableEquates().addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {

            System.out.println("key pressed!!");
            controller.getStandardActionDetailsTableEquates().setRowSelectionAllowed(true);
            int selectedRow = controller.getStandardActionDetailsTableEquates().getSelectedRow();
            System.out.println("selectedRow : " + selectedRow);

            controller.getStandardActionDetailsTableEquates().requestFocusInWindow();

            if (e.getKeyCode() == KeyEvent.VK_UP) {
                System.out.println("up arrow key pressed");

                Component editor = controller.getStandardActionDetailsTableEquates().getEditorComponent();
                editor.requestFocusInWindow();
                System.out.println("cursor : " + controller.getStandardActionDetailsTableEquates().getCursor());
                System.out.println("value : " + controller.getStandardActionDetailsTableEquates().getSelectedRow());

            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                System.out.println("down arrow key pressed");
            }

        }
    });
}

I even tried to get the cursor position, but could not get it. Also, the editor found did not worked (shown in code). The "selected row" and "value" values are pining as "-1".

我甚至试图找到光标的位置,但无法得到它。而且,发现的编辑器没有工作(如代码所示)。“选定的行”和“值”的值渴望为“-1”。

Please provide solution to this.

请提供解决方案

1 个解决方案

#1


2  

Don't use a KeyListener; use Key Bindings. JTable has the following default bindings in the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT input map:

不要使用KeyListener;使用键绑定。JTable在WHEN_ANCESTOR_OF_FOCUSED_COMPONENT输入映射中有以下默认绑定:

  • VK_UP has the key "Table.selectPreviousRow"

    VK_UP有一个键“Table.selectPreviousRow”

  • VK_DOWN has the key "Table.selectNextRow".

    VK_DOWN有一个键“Table.selectNextRow”。

You can replace the bindings with Action instances that update your TableModel as desired. Alternatively, consider a custom Comparator in your RowSorter, as shown here.

您可以用更新TableModel的操作实例替换绑定。或者,考虑一个自定义比较器,如这里所示。

#1


2  

Don't use a KeyListener; use Key Bindings. JTable has the following default bindings in the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT input map:

不要使用KeyListener;使用键绑定。JTable在WHEN_ANCESTOR_OF_FOCUSED_COMPONENT输入映射中有以下默认绑定:

  • VK_UP has the key "Table.selectPreviousRow"

    VK_UP有一个键“Table.selectPreviousRow”

  • VK_DOWN has the key "Table.selectNextRow".

    VK_DOWN有一个键“Table.selectNextRow”。

You can replace the bindings with Action instances that update your TableModel as desired. Alternatively, consider a custom Comparator in your RowSorter, as shown here.

您可以用更新TableModel的操作实例替换绑定。或者,考虑一个自定义比较器,如这里所示。