• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CellRenderer on the fly

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a CellRenderer that colors every other row of a table with a different color. What I need is to color some rows with a third color, but this needs to be done on the fly since every time I load a table, a row that needs to be colored in another color will change.
Any suggestions are appreciated.
Here is the renderer:
---------------------
class StyleColorCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table,
Object val,
boolean isSelected,
boolean hasFocus,
int row,
int column){
Component cell = super.getTableCellRendererComponent(table, val, isSelected, hasFocus, row, column);
int rowCount = table.getRowCount();
boolean yellow = true;
if(row % 2 == 0)
cell.setBackground(new Color(255,255,206));
else
cell.setBackground(new Color(209,210,211));
if(hasFocus)
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
setHorizontalAlignment(SwingConstants.RIGHT);
return cell;
}
}
thanks,
Alex
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this third color determined? Can it be determined from the value object that is being passed in?
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I already sovled this one. I pass array of integers representing number of rows to CellRenderer, then I check if current row is from array. If it is I color it white, if not I color it alternating colors.
thanks,
Alex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic