使用static方法,放在常用工具类myUtil中,
只需在表格数据生成后加入myUtil.makeFace(jTable1);即可:
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)); //设置偶数行底色
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();
}
}
//...省略jTable数据显示代码...
//表格数据生成后加入:
myUtil.makeFace(jTable1);