I use this code to color the rows of ly jtable by differents colors :
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(row % 2 == 0 ? Color.WHITE : Color.LIGHT_GRAY);
return c;
}
});
it works, now I want to color the row selected by the user to another color diferent than those above with :
table.setSelectionBackground(Color.RED);
but it does anything How can I achieve this ?
thank you in advance
Your renderer is overriding the color change applied by the DefaultTableCellRenderer
Try something like...
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (!isSelected) {
c.setBackground(row % 2 == 0 ? Color.WHITE : Color.LIGHT_GRAY);
}
Instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With