File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable Focus Subsystem and KeyBindings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Focus Subsystem and KeyBindings" Watch "JTable Focus Subsystem and KeyBindings" New topic
Author

JTable Focus Subsystem and KeyBindings

Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 112
Hi there,

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!

Carlos.
Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 112
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:

setColumnSelectionInterval(c,c,true,false)
setRowSelectionInterval(r,r,true,false)

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!

thank you again.
Carlos.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable Focus Subsystem and KeyBindings
 
Similar Threads
Problem with a JTable
JTable Cell Focus
Forcing a cell selection in a JTable
Jtable as listbox?
JTable with different objects (or components) in a column