public static void makeFace(JTable table) {
try {
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if (row % 2 == 0) {
setBackground(Color.white); //設置奇數行底色
} else if (row % 2 == 1) {
setBackground(new Color(206, 231, 255)); //設置偶數行底色
}
if (Double.parseDouble(table.getValueAt(row, 11).toString()) > 0) {
setBackground(Color.red);
}
//如果需要設置某一個Cell顏色,需要加上column過濾條件即可
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}
};
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}