Here is what I want. I want tab to go to the next editable cell and shift+tab to go to the previous editable field. I think I may be going down a terrible path right now from some code I found out there.
Right now, I'm extending JTable
and overriding the changeSelection
method. I'm looking at the current event and saying "if this is a tab event, go to the next editable field". This works, except in the case where the user is currently editing and then they hit tab. In that case, a HierarchyEvent happens with (as far as I can tell) no indication that the hierarchy change came from a tab or a mouse click, or whatever. So it defaults to moving to the next cell (which is usually uneditable).
Here is what I'm currently doing
private final KeyStroke tabKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
@Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
{
AWTEvent currentEvent = EventQueue.getCurrentEvent();
if (currentEvent instanceof KeyEvent)
{
KeyEvent keyEvent = (KeyEvent) currentEvent;
if (keyEvent.getSource() == this && KeyStroke.getKeyStrokeForEvent(keyEvent).equals(tabKeyStroke))
{
Row nextEditableCell = getNextEditableCell(rowIndex, columnIndex);
if (nextEditableCell != null)
{
rowIndex = nextEditableCell.getRow();
columnIndex = nextEditableCell.getColumn();
}
}
}
super.changeSelection(rowIndex, columnIndex, toggle, extend);
}
How can I make my JTable, on tabs whether it is currently editing or not, shift to the next editable cell and not just the next cell?