【发布时间】:2013-09-29 17:42:05
【问题描述】:
我想同时为 jtable 中的特定单元格着色
例如,column = 1 和 row i+2 的单元格(i 从 0 到 5)。
我成功地使用CustomTableCellRenderer 为特定单元格着色,如示例所示
public class CustomTableCellRenderer extends DefaultTableCellRenderer
{
int x;
int y;
CustomTableCellRenderer(int x,int y){ //constructor
this.x= x;
this.y=y;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
if ( ((row == x) && (column == y))) { //test of equivalence of x and y as parameter
cell.setBackground(Color.green);
}
else {
cell.setBackground(Color.WHITE);
}
return cell;
}
}
【问题讨论】: