1. 设置指定行的颜色
class TableColorRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
String ss = table.getModel().getValueAt(row, 1).toString();
for(int i=0; i<table.getColumnCount(); i++) {
if(ss.equals("期初余额")) {
super.setBackground(Color.RED);
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, i);
} else if (ss.equals("本日合计") || ss.equals("本期合计")) {
super.setBackground(Color.BLUE);
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, i);
} else if (ss.equals("本年累计")) {
super.setBackground(Color.PINK);
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, i);
} else {
super.setBackground(Color.GREEN);
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, i);
}
}
return this;
}
}
table.setDefaultRenderer(Object.class, new TableColorRenderer());