This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes disabling table cells for selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "disabling table cells for selection" Watch "disabling table cells for selection" New topic

disabling table cells for selection

Nadin Jovan

Joined: Aug 12, 2003
Posts: 7
I have a table in which main diagonal should be disabled for selection.
Users should be able to click on the remaining cells in order to turn them on or off. I would like to disable the main diagonal so that it is clear to the user that those cannot be changed.
What I did is, I created a subclass of the AbstractTableModel class and in that class I overwrote isCellEditable(int row, int column) method as follows:
public boolean isCellEditable(int row, int col) {
boolean result = (row >= 0 && col >= 1 && row != (col - 1));
return result;
So this will not allow user to modify cells on the main diagonal. However, these cells look the same as the rest of the cells in the table (they are always off and they cannot be modified). I would like to disable them in some obvious and visible way, like when a CheckBox is disabled. Is that possible to do?
Joe Ess

Joined: Oct 29, 2001
Posts: 9168

I'm just throwing this out there, but I think you have to extend JTable and override getCellRenderer(int, int). Then you need to implement javax.swing.table.TableCellRenderer or extend javax.swing.table.DefaultTableCellRenderer to make a DisabledCellRenderer. Use setBackground() and setForeground() in DisabledCellRenderer to make your disabled cells unique. In getCellRenderer() you can return DefaultTableCellRenderer for the active cells and DisabledCellRenderer for the disabled ones.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: disabling table cells for selection
It's not a secret anymore!