JTable 单元格渲染器不会只更改指定的单元格
JTable cellRenderer doesnt change just desingated cells
这是我项目的代码部分。
我正在尝试更改指定单元格的颜色。但是当我尝试时,所有单元格的颜色都会改变。这是为什么 ?
谢谢
private class cellRenderer extends DefaultTableCellRenderer {
@Override
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==column){
cell.setBackground(Color.yellow);
}
return cell;
}
}
我觉得你需要恢复原来的颜色。
private class cellRenderer extends DefaultTableCellRenderer {
Color originalColor = null;
@Override
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 (originalColor == null) {
originalColor = cell.getBackground();
}
if(row==column){
cell.setBackground(Color.yellow);
} else {
cell.setBackground(originalColor);
}
return cell;
}
}
这是我项目的代码部分。 我正在尝试更改指定单元格的颜色。但是当我尝试时,所有单元格的颜色都会改变。这是为什么 ? 谢谢
private class cellRenderer extends DefaultTableCellRenderer {
@Override
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==column){
cell.setBackground(Color.yellow);
}
return cell;
}
}
我觉得你需要恢复原来的颜色。
private class cellRenderer extends DefaultTableCellRenderer {
Color originalColor = null;
@Override
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 (originalColor == null) {
originalColor = cell.getBackground();
}
if(row==column){
cell.setBackground(Color.yellow);
} else {
cell.setBackground(originalColor);
}
return cell;
}
}