I am trying to extend a JTable component with a specific focus and cell selection dynamics. The cell selection highlight should move from left to right upon Enter stroke and jump some columns over (these columns are fixed). When reaching the last focusable cell on a row it must jump to the leftmost focusable cell in the next row.
Sounds easy but have been stuck a while now. I have used to approaches: firstly have tried to attach a custom FocusCyclePolicy on the table, setting it as a FocusCycleRootProvider and enabling the policy. Didn't work. The policy overriden methods were not called at all, no matter what. Secondly I tried to implement a keybinding on the InputMap(JComponent.WHEN_ANCESTOR_COMPONENT_IS_FOCUSED) and attach the Action that would manually check the current row/column and push the selection accordingly, but then my CellEditors stopped working! hahaha... should have guessed....
this is the most I can think of in order to implement it in a neat fashin according to Swing practices....
Any ideas? I presume the FocusCyclePolicy is the best choice, since focus should be managed after keybindings are triggered, not altering the behaviour of my celleditors. However I am not being able to attach the focus policy. Is it because a container is overtaking that. Can't I override that?
Thanks for taking the time to read me and for your advise in advance!
Joined: Apr 21, 2010
Cool! got it!
finally I came across a post which fully clarified my understanding on this very little issue and convinced me to further investigate the keybindings. That was the solution. Focus was confusing me, but in fact although most of us know that table is a single component instead of a sum of cell components, somehow I thought focus had to be the point, but it was not, since table is a "single" component, therefore establishing a focus policy is useless, and although the methods are there to attach focus policies I got none to be called, which means that somewhere the Event Dispatching Thread must skip it from the focus management (on this if anyone has anything to say, any advise is greatly appreciated to deepen my understanding in the Swing thread schema). So basically what I did was to override the ENTER keybinding associated with the table ("selectNextRowCell") and attach a custom action to the Action-/Inputmap which would manually handle selection by calling the following methods in the JTable:
which completely fullfilled my needs. By the way additionally setRowSelectionAllowed and setColumnSelectionAllowed must be set to false, in order to avoid full column/row selection, in favor of single cell selection.
Hope this helps someone. So stupid but took so much time! Hope no one else will spend as much as I did!