我想在单击按钮时更改JTable
中单个单元格的颜色。
请参阅下图中的示例
我尝试使用此代码实现此目的:
drig_rdv.getModel().setValueAt("Anouar", 0, 2);
MyCellRenderer mcr = new MyCellRenderer();
for (int columnIndex = 0; columnIndex < drig_rdv.getColumnCount(); columnIndex ++) {
drig_rdv.getColumnModel().getColumn(columnIndex).setCellRenderer(mcr);
}
这是我的单元格渲染器:
public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
cellComponent.setBackground(java.awt.Color.YELLOW);
return cellComponent;
}
}