I've already read a lot about CellRendering in Java and I have also visited other Q&As from this great site. Unfortunately I still haven't found the solution for the following Problem:
I want to render a JTable which displays StatusEvents - this is necessary for monitoring a running System. However, those StatusEvents consist of a timestamp, a text and a color.
My goal is it to enable multiple colored rows. To achieve this, I've already defined a new JTable-subclass (Overloading "getCellRenderer related to the Row which is being painted during the inseration process) and a new TableCellRenderer-Subclass, which applies the Color to the Cell.
the Methods look like the following:
MyCustomJTable:
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
TableCellRenderer result = super.getCellRenderer(row, column);
if ( row == 0 )
{
result = colcr;
}
return result;
}
colcr is my custom CellRenderer which is coloring a Cell in a specific color which is being set before.
The new Cell Renderer looks like the following:
public class ColorCellRenderer extends DefaultTableCellRenderer {
ColorCellRenderer ( )
{
this.m_Color = null;
}
@Override
public Component getTableCellRendererComponent ( JTable table , Object value , boolean isSelected ,
boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
if ( m_Color != null )
{
if ( row == 0 && column == 0)
{
c.setForeground(m_Color);
}
}
return c;
}
public void setColor ( Color c )
{
this.m_Color = c;
}
private Color m_Color;
}
Unfortunately the current solution colors just the first row in the latest configured color, but the previously colored rows lose their color and get formatted by default.
Which possibilities do I have to avoid this behaviour?
sincerely
Markus