0
我的問題是我想更改jTable中一列中單元格的單獨背景顏色。我提出的代碼將顏色更改爲一個,它適用於所有列。我究竟做錯了什麼?如何使用CustomRenderer更改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;
}
}