我有自定义TableCellRenderer
public class CustomCellRenderer extends JLabel implements TableCellRenderer {
private TableCellRenderer defaultCellRenderer;
public CustomCellRenderer(TableCellRenderer defaultCellRenderer) {
this.defaultCellRenderer = defaultCellRenderer;
}
public Component getTableCellRendererComponent(JTable table, Object ovalue, boolean isSelected, boolean hasFocus, int row, int column) {
String val = (String) ovalue;
Component c = defaultCellRenderer.getTableCellRendererComponent(table, ovalue, isSelected, hasFocus, row, column);
if (c != null) {
if (val.equals("0h")) {
c.setForeground(Color.RED);
} else {
c.setForeground(table.getForeground());
}
return c;
} else return null;
}
}
...
TableCellRenderer renderer = new CustomCellRenderer(sumTable.getDefaultRenderer(columnModel.getColumn(i).getClass()));
columnModel.getColumn(i).setCellRenderer(renderer);
and it worked normal util I need change cell Background Color it sets Color not in certain cells and in all column, all cells.
...
if (val.equals("0h")) {
c.setBackground(Color.GRAY);
} else {
c.setForeground(table.getForeground());
}
...
我需要做什么?
最佳答案
在您的else语句中,您需要将背景设置为某种背景。例如。 :c.setBackground(Color.WHITE);