jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes JTable Selection Depends On Editable Cells Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Selection Depends On Editable Cells" Watch "JTable Selection Depends On Editable Cells" New topic
Author

JTable Selection Depends On Editable Cells

John Smith
Ranch Hand

Joined: Jul 20, 2001
Posts: 84
Hi There,
When I single click a cell in a JTable which is not editable, the cell is painted blue. When I click a cell which is editable, a blue border appears around the cell, but the cell itself remains white. Does anyone know how to alter this behavior? I want an editable cell to look like a non-editable cell when it is clicked (I want everything painted blue when clicked - not just the border). When you click on a cell and it isn't painted blue, it gets confusing (people think it's not selected).
I have a feeling this is related to the CellRenderer's. For an editable cell it may be a TextField (when clicked only the border is shown) and a non-editable cell may have a JLabel (when clicked the entire thing is painted). I may be wrong about this though. Thanks for any help...
Randy
John Smith
Ranch Hand

Joined: Jul 20, 2001
Posts: 84
Okay, I answered my own question. In case any one is interested, I simply created my own renderer (which extended from DefaultTableCellRenderer) which basically used the same code for GetTableCellRendererComponent() as the DefaultTableCellRenderer except I got rid of these lines of code:

[ January 12, 2002: Message edited by: Randy Gordon ]
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
You can also set the selection mode of the table to single row selection...like this:
JTable table = new JTable(<add model, etc> ;
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
When you click on a row after doing this, the entire row is selected, with no distinction between the row and the cell you selected.
-Jeff


Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
 
Don't get me started about those stupid light bulbs.
 
subject: JTable Selection Depends On Editable Cells
 
Similar Threads
JTable Cell
JTable selection problem
Update Jtable
JTable
getting focus in one cell in JTable