Focus issue in JTable with custom component

Hi,

Spent all day on figuring out how this works but things only got worse... feeling like an idiot!

The problem is as follows: I have a JTable that has different types of components in the columns, usually it is two JTextFields followed by two custom components ("TimeField", which can include up to 3 JTextFields for hours, minutes, and seconds). The time input components can be hidden by removing the time columns, but that just as a side note, this doesn't seem to be a problem.

What I try to achieve is that the user is able to tab through the table, from one TextField to the next, and then cycle through the time input textfields, and then continue with the next "normal" textfield in the next row.

I tried this with the use of FocusTraversalPolicies, FocusListeners, PropertyListeners and who knows what else, nothing worked.

About the custom time components, they are inherited from JPanel, and have another JPanel in them, which again contains the three JTextFields (with Labels ":" between them).

I can provide code samples but I think what I need is a basic brief explanation how focus traversal REALLY works, since the event threads (and aparently some bugs in Java?) make my life hard and whatever approach I try it fails for a different reason. I read a couple of online tutorials and a book, to no avail.

Any help GREATLY appreciated :-)

Thanks!

[1419 byte] By [Anigart_Xa] at [2007-11-26 22:00:52]
# 1

If you're talking about the focus traversal inside a JTable, it is handled by the UI class, which uses [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]Key Bindings[/url] to intercept the TAB key and move the focus to the next column.

Take a look at BasicTableUI.java - selectNextColumnCell action.

It is using the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT map for the key bindings:

Object obj = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).get(KeyStroke.getKeyStroke("TAB"));

If you want to have a custom focus traversal you can register your own key binding for TAB key and handle it yourself.

Rodney_McKaya at 2007-7-10 10:35:57 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi and thanks for your reply,

Traversal between the table cells as such is no problem. The issue is that not all cell editors are made of the same JComponent type. Some are JTextFields (-> traversal between these works fine), and some are Containers which contain several other JComponents (between which I'd like to tab). These containers are for entering times (JTextFields for h/m/s).

I cannot traverse focus downwards and back upwards to and from the default component of the time field Containers when tabbing into or out of such a cell. One issue might be that the Container only contains one JPanel, which by itself contains the JTextFields.

I tried cycleFocusUp and down twice to get from the Container over the JPanel to the first JTextField but for some reason it doesn't work.

Anigart_Xa at 2007-7-10 10:35:57 > top of Java-index,Desktop,Core GUI APIs...