I have a JTable
that I need to change the traversal inside such that a TAB keystroke advances row by row. (Normally, the TAB keystroke advances cell by cell.) I was able to change the forward traversal on the TAB keystroke. I tried the same thing for reverse traversal on SHIFT+TAB. I can't capture the 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);
How do I capture SHIFT+TAB in a JTable
?
use
or use
instead of
And it should work.
The documentation of
KeyStroke.getKeyStroke(String s)
states