# 表格自定义颜色
# 代码
// 渲染
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// 判断特定列和行
if (column == 4) { // 如果是第四列
Object statusValue = table.getValueAt(row, column);
// 根据特定条件设置单元格的背景颜色
if (statusValue != null && statusValue.toString().equals("空闲")) {
component.setBackground(Color.decode("#2d9383"));
} else if (statusValue != null && statusValue.toString().equals("使用中")) {
component.setBackground(Color.decode("#FFA500"));
} else {
component.setBackground(table.getBackground());
}
} else {
// 对于其他列,保持原来的背景颜色
component.setBackground(table.getBackground());
}
return component;
}
});