Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

disabling table cells for selection

 
Nadin Jovan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks.
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic