我想改变JTable中单个单元格的颜色

时间:2016-04-21 18:11:49

标签: java jtable tablecellrenderer

我想在单击按钮时更改JTable中单个单元格的颜色。

请参阅下图中的示例

enter image description here

我尝试使用此代码实现此目的:

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;
   }
}

0 个答案:

没有答案