gpt4 book ai didi

java - 我能够捕获 jtable 中的 TAB,但如何捕获 SHIFT+TAB?

转载 作者:行者123 更新时间:2023-11-30 03:23:04 25 4
gpt4 key购买 nike

我有一个 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?

最佳答案

使用

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) 的文档州

Parses a string and returns a KeyStroke. The string must have the following syntax:

<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_".

If typed, pressed or released is not specified, pressed is assumed. Here are some examples:

 "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');

关于java - 我能够捕获 jtable 中的 TAB,但如何捕获 SHIFT+TAB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30871495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com