I want to change the background color of particular table header. In my appliaction I have to set header color Red on the current month.
My Code is here::
jTable1.getTableHeader().
setDefaultRenderer(
new DefaultTableHeaderCellRenderer());
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
JTableHeader tableHeader = table.getTableHeader();
if(column==1)
tableHeader.setBackground(Color.red);
return this;
}
this make all the header color's red. Please give me some suggestion. Thanks in advance.
The infamous color memory of DefaultTableCellRenderer :-) You have to
something like:
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
if (myHighlightCondition) {
setBackground(Color.RED);
} else {
setBackground(null);
}
super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, column);
return this;
}
For more details (and why that's needed) see a How do I correctly use custom renderers to paint specific cells in a JTable?
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