我能够在jtable中捕获TAB,但是如何捕获SHIFT + TAB?

时间:2015-06-16 15:08:56

标签: java swing key-bindings key-events

我有一个JTable我需要更改内部的遍历,以便TAB键击逐行前进。 (通常,TAB击键逐个单元地进行。)我能够在TAB键击上改变向前遍历。我在SHIFT + TAB上尝试了相同的反向遍历。我无法捕获SHIFT + TAB。

    InputMap im = myTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    ActionMap am = myTable.getActionMap();

    // make shift+tab row to row instead of cell to cell
    Action shiftTabActionmyTable = new AbstractAction("SHIFT+TAB")
    {
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!inside");
            int rowView = myTable.getSelectedRow();
            int numRows = myTable.getRowCount();

            if (0 < numRows)
            {
                if ((-1 == rowView) || (0 == rowView))
                {
                    // Move to last row
                    rowView = numRows - 1;
                }
                else
                {
                    // Move to prev row
                    rowView--;
                 }

                myTable.changeSelection(rowView, 0, false, false);
                myTable.scrollRectToVisible(myTable.getCellRect(rowView, COL_ICON, true));
            }
        }
    };
    KeyStroke VK_Shift_Tab = KeyStroke.getKeyStroke("SHIFT+TAB");
    im.put(VK_Shift_Tab, shiftTabActionmyTable.getValue(Action.NAME));
    am.put(shiftTabActionmyTable.getValue(Action.NAME), shiftTabActionmyTable);

    System.out.println("!!!!!!!!!!!!!!!!!!!!!!Name " + shiftTabActionmyTable.getValue(Action.NAME));


    // Make tab row to row instead of cell to cell
    Action tabActionmyTable = new AbstractAction("TAB")
    {
        public void actionPerformed(ActionEvent e)
        {
            int rowView = myTable.getSelectedRow();
            int numRows = myTable.getRowCount();

            if (0 < numRows)
            {
                if ((-1 == rowView) || ((numRows - 1) == rowView))
                {
                    // Move to first row
                    rowView = 0;
                }
                else
                {
                    // Move to next row
                    rowView++;
                }

                myTable.changeSelection(rowView, 0, false, false);
                myTable.scrollRectToVisible(myTable.getCellRect(rowView, COL_ICON, true));
            }
         }
    };
    KeyStroke VK_Tab = KeyStroke.getKeyStroke("TAB");
    im.put(VK_Tab, tabActionmyTable.getValue(Action.NAME));
    am.put(tabActionmyTable.getValue(Action.NAME), tabActionmyTable);

如何在JTable中捕获SHIFT + TAB?

1 个答案:

答案 0 :(得分:2)

使用

KeyStroke VK_Shift_Tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
            InputEvent.SHIFT_DOWN_MASK);

或使用

KeyStroke VK_Shift_Tab = KeyStroke.getKeyStroke("shift TAB");

而不是

KeyStroke VK_Shift_Tab = KeyStroke.getKeyStroke("SHIFT+TAB");

它应该有用。

KeyStroke.getKeyStroke(String s)州的文件

  

解析字符串并返回KeyStroke。字符串必须有   语法如下:

<modifiers>* (<typedID> | <pressedReleasedID>)

modifiers := shift | control | ctrl | meta | alt | altGraph
typedID := typed <typedKey>
typedKey := string of length 1 giving Unicode character.
pressedReleasedID := (pressed | released) key
key := KeyEvent key code name, i.e. the name following "VK_".  
     

如果未指定键入,按下或释放,则按下。   以下是一些例子:

 "INSERT" => getKeyStroke(KeyEvent.VK_INSERT, 0);
 "control DELETE" => getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
 "alt shift X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
 "alt shift released X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
 "typed a" => getKeyStroke('a');