Reputation: 351
Summary:
In my desktop application i load one Jtable and when in edit mode if i press tab i need the focus of the cell on to the next cell.
Problem: When i am editing the value of a cell and then when i press Tab the focus is lost. I did some search on the net and i found that it happens because on each Tab press the Jtable reloads itself.
Possible Solution One solution that i was thinking of is to get the indices of the cell i am working in, same it in a global variable and then on Tab press i can get the indices of the next cell and set focus on that cell. Somehow it didn't work.
Please suggest.
Thanks in advance..
Upvotes: 3
Views: 7223
Reputation: 956
Normally tab works in jTable once getting the focus .If you want to edit next cell by pressing Tab key give the following code in the key release event of jTable.
if (evt.getKeyCode() == 9) {
jTable1.editCellAt(nextRowIndex, nextColumnIndex);
}
Upvotes: 3
Reputation: 347204
Off the top of my head, I think we overcame this with a custom keystroke
implementation in the tabes InputMap
& ActionMap
.
The implementation we use allows us to perform "continuous" editing, that is, when the user presses enter or tab, we move to the next editable cell and start editing
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = table.getActionMap();
KeyStroke tabKey = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
Action moveNextCellAction = am.get(im.get(tabKey));
ContinousEditAction continousEditAction = new ContinousEditAction(table, moveNextCellAction);
im.put(tabKey, "Action.tab");
am.put("Action.tab", continousEditAction);
The ContinousEditAction
is responsible for finding the next editable cell. Basically when the action is fired, you take stock of the current cell via JTable.getEditingRow
& JTable.getEditingColumn
methods (you also want to check that the table is edit mode via JTable.isEditing
, otherwise you need to use JTable.getSelectedRow
& JTable.getSelectedColumn
- in fact you might get away with doing just this, but this is how I approached the problem).
From there, you want to walk the cells until you find a cell that is editable.
Basically, you want to check to the end of the current row, then move to the next until no more rows exist, depending on what you want to do, you may choose to loop back around to the start of the table (cell 0x0) and walk it until you reach your current position.
Be careful, you can end up in a continuous loop if you're not careful :P.
If you don't find any editable cells, you may simply wish to select the next available cell using JTable.setRowSelectionInterval
& JTable.setRowSelectionInterval
, other wise you can call JTable.editCellAt(nextRow, nextCol)
But this all comes down to what it is you want to achieve.
Also, you can apply the same idea to the enter key ;)
Upvotes: 5