aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Disabling JTable Cell selections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Disabling JTable Cell selections" Watch "Disabling JTable Cell selections" New topic
Author

Disabling JTable Cell selections

Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
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
Sayuri Coppinger
Greenhorn

Joined: Jun 06, 2001
Posts: 20
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
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
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
Sayuri Coppinger
Greenhorn

Joined: Jun 06, 2001
Posts: 20
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
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
Thanks...
cs sakthi
Greenhorn

Joined: May 11, 2001
Posts: 15
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
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
changeSelection() is under JTable in the API.
 
wood burning stoves
 
subject: Disabling JTable Cell selections