• Post Reply Bookmark Topic Watch Topic
  • New Topic

jTable row color change "if"  RSS feed

 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

In my jTable1, column 4 (Dialed), I need to see if the latest entry to the "Dialed" column equals "911"  .... if so I want to change the entire row to a RED background color to highlight this row.

In this code shown, I'm not getting any intended color change effect.





....... calling this cass up with  " new MyTableCellRenderer(); "  

The problem I think is that it doesn't know what JTable i'm referring to  ..... but I dont know how to tell it so  :  (

Any attempt I try to change " JTable "  to my "jTable1"  ...... It doesn't care you that.
 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...... Do I need to pass the JTable1 to this class(myTableCellRender)  ..... then do something with he return info??
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Table Row Rendering for an approach that overrides the prepareRenderer(...) method to provide custom rendering for the entire row.
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried running your code or anything, but I have some comments:

1) I see you are calling convertRowIndexToModel(), but your code as it stands really needs to convertColumnIndexToModel() as well. [But see comments #5 and #6, below.]

2) It doesn't work to call setForeground() and subsequently call super.getTableCellRendererComponent(), because super.getTableCellRendererComponent() will set a foreground color that clobbers yours.

3) It is probably a mistake to call setForeground(null). Better to call setForeground(Color.BLACK) or setForeground(table.getForeground()) or setForeground(isSelected ? something : table.getSelectionForeground()); [Issue #2 is shielding you from this mistake.]

4) Merely instantiating an instance of MyTableCellRenderer is not enough. There are a lot of different ways to make sure your renderer is actually used. Here's one: yourTable.getColumnModel().getColumn(4).setCellRenderer(new MyTableCellRenderer()).

5) If you use the renderer registration method I mentioned in comment #4, then your cell renderer doesn't need to do any checking on the column number. Better and more reusable to remove that code from the renderer.

6) There is no need to call table.getModel().getValueAt(row, column). That's what the method's value parameter is for.
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it allowed me to edit my post I would change setForeground(isSelected ? something : table.getSelectionForeground()) to setForeground(isSelected ? table.getSelectionForeground() : something).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!