Tab into JTable with one tab instead of two?

Tabbing into a JTable only almost works; the first time you hit tab from the immediately prior component the cursor just disappears, and it takes a second tab to get it into the JTable. I've found out that Swing thinks the focus is in the JTable even after first tab, so JTable is somehow hiding the focus in limbo until you say "no, I _really_ want the focus to be in you", so to speak.

There's a simple example that replicates this bug here (don't even need to use custom focus traversal, shows up with normal):

http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html#customFocusTraversal

But I couldn't find anything on Google about it. Is there a known workaround for this? I tried tinkering with editCellAt, selectAll etc but no effect.

Thanks,

[792 byte] By [r.wallacea] at [2007-11-27 7:45:32]
# 1
Works fine for me using JDK1.4.2 on XP.
camickra at 2007-7-12 19:26:13 > top of Java-index,Desktop,Core GUI APIs...
# 2
Oh, hmm. I'm using 1.6 with XP. Maybe that means it's a recently introduced bug?
r.wallacea at 2007-7-12 19:26:13 > top of Java-index,Desktop,Core GUI APIs...
# 3

I think the problem is when a JTable is created there is no selected cell, and no current position. Focusable elements remember the latest position the cursor was in; if this does not point to any cell, the table itself will be focused only. At a second Tab the first cell will be focused. Once a cell was selected, the focusing will work as expected for the remainig time of execution, the focus will always return to the last clicked cell, not depending on the selections made.

Is this what you mean?

sztyopeka at 2007-7-12 19:26:13 > top of Java-index,Desktop,Core GUI APIs...
# 4
As a workaround you could try adding a key listener onto the previous component and check for KeyEvent.VK_TAB and in that event you can use the requestFocusInWindow() and changeSelection() methods of JTable to select the first cell
c0demonk3ya at 2007-7-12 19:26:13 > top of Java-index,Desktop,Core GUI APIs...
# 5
Ah yes, that seems to have done the trick, thanks! Specifically, I just tried at table creation time: table.changeSelection(0, 0, true, false);and that seems to work to select the first cell as soon as you tab into the table.
r.wallacea at 2007-7-12 19:26:13 > top of Java-index,Desktop,Core GUI APIs...