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.lightGray); // 设置奇数行底色
else if (row % 2 == 1)
setBackground(Color.white); // 设置偶数行底色
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();
}
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.lightGray); // 设置奇数行底色
else if (row % 2 == 1)
setBackground(Color.white); // 设置偶数行底色
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();
}
}
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/308274,如需转载请自行联系原作者