I have a custom editor and renderer for a JTable cell to display multiple components e.g textfield, combobox.
When tabbing , the focus moves from cell to cell but I would like to transfer focus to the first component in this particular cell.
I tried to use requestFocus() but the jpanel holding these components is not displayable so it does not work.
Can anybody suggest a solution to this focus problem?
The problem could be this part of JTable-code:
public Component prepareEditor(TableCellEditor editor, int row, int
column) {
Object value = getValueAt(row, column);
boolean isSelected = isCellSelected(row, column);
Component comp = editor.getTableCellEditorComponent(this, value,
isSelected,
row, column);
if (comp instanceof JComponent) {
JComponent jComp = (JComponent)comp;
if (jComp.getNextFocusableComponent() == null) {
jComp.setNextFocusableComponent(this); // <-- !!!
}
}
return comp;
}
Means, every time your editor is accessed, nextFocusableComponent is
called on it (nextFocusableComponent is actually deprecated, but you
still find this code in JDK 1.4 / 1.5...), setting the table as next
component. I don't know what the JPanel is doing when it's
setNextFocusableComponent() is used, since a panel normally isn't part
of the focus traversal. You might override prepareEditor(), and change
the jComp.setNextFocusableComponent(this); to something like (
(MyPanel) jComp).getLastComponent().setNextFocusableComponent(this);.
-Tom
Pooja B - 21 Jan 2005 17:36 GMT
Hi Tom,
Thanks for the reply.
Actually I have a Jpanel table cell.Lets say it has 2 comboboxes and a textfield.
When I tab to this cell, I would like to have focus on the first combobox and then traverse through to second and then textfield .
When the textfield has focus and tab is pressed I would like to transfer the focus to next cell.
Right now tabbing just allows to transfer focus from cell to cell but not within the cell containing jpanel.
Tom - 24 Jan 2005 10:06 GMT
Ok, have a look at the InputMap / ActionMap of the JTable, which is
usually set within the TableUI. InputMap maps an action (e.g. pressing
of the tabKey) to an action-String ("selectNextCell"). The ActionMap
maps this action to an Action (derived from ActionListener). You could
map the VK_TAB to your own action-string, and write your own Action
implenetation that traverses the selection to the next cell, or, in
case you're on your special cell editor, requests the focus for the
first component of your editor.
hth, Tom