This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a JTable that contains several columns of data that I want to not be selectable. What I am wanting to achieve is that when the user uses the tab key to move around the table for these cells/columns to be skipped. That way only the cells that can be edited are tabbed to whenever the tab key is used for navigation. Does anyone have any ideas here on how I can do this? Lon Allen
I came across the same problem and struggled with it for a couple of days, but finally got it figured out. You need to implement isCellEditable(row,col) in your data model to return the appropriate boolean flag for the cells. Then add a key listener to the table and implement the keyReleased method to check for VK_TAB key code, if it is the tab key, then loop through your table, checking to see if the cell is editable. If editable, call changeSelection method to change to the next editable cell. The logic is actually a bit more involved than this, but this should get you started. The other thing I would do is add a focus listener to clear selection on focus lost and change selection to the first editable cell on focus gained. Also, if you are planning to tab out of the table using the tab key as opposed to CTRL TAB, then you need to use the focus manager when you are tabbing out of the last editable cell. Good luck! Sayuri
Joined: May 24, 2001
Thanks for the ideas. I had thought that I might need to do something along these lines, but was hoping that somehow it might be easier. Lon Allen
Joined: Jun 06, 2001
Yeah, I thought the same thing but no such luck. BTW, if your table is contained in a scrollpane, you might run into some problems traversing the table with the tab key. You will need to tab twice and sometimes even 3 times! This is a known bug. I actually ended up taking my table out of the scrollpane to get the behaviour I wanted. Just some things to keep in mind....Like I said I did struggle to get it working
Hello, I am also in need of disabling the JTable cells, and to traverse thro' only the editable cells of Jtable. I found the discussions in this thread could give me a solution. But i am struggling where to start, and i couldn't find the method changeSelection() in javadoc api. Could u help me... with some samples Thanks in advance, -Sakthi