我的问题是我想更改 jTable 中一列中单元格的单个背景颜色。我提出的代码将颜色更改为一种,它适用于所有列。我究竟做错了什么?
这是我的代码
public void fillReserveTable() {
MemberDAO dao = DATA.MemberDAO.getInstance();
ResultSet res2 = dao.fillReservationTable();
try {
if (res2.next()) {
res2.beforeFirst();
reserveTable.setModel(DbUtils.resultSetToTableModel(res2));
setUpOnHold(reserveTable, reserveTable.getColumnModel().getColumn(4));
reserveTable.getColumnModel().getColumn(3).setCellRenderer(new CustomRenderer());
jScrollPane14.setVisible(true);
}else{
jScrollPane14.setVisible(false);
}
} catch (SQLException e) {
}
}
class CustomRenderer extends DefaultTableCellRenderer {
MemberDAO dao = DATA.MemberDAO.getInstance();
ResultSet res2 = dao.fillReservationTable();
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component cellComponent = super.getTableCellRendererComponent(reserveTable, value, isSelected, hasFocus, row, column);
int row2 = 0;
try {
while (res2.next()) {
String status = reserveTable.getValueAt(row2, 3).toString();
if (status.equals("Available")) {
cellComponent.setBackground(Color.green);
} else {
cellComponent.setBackground(Color.red);
}
row2++;
}
} catch (SQLException e) {
}
return cellComponent;
}
}