Java JTable change cell color

后端 未结 7 1520
说谎
说谎 2020-11-30 14:14

I would like to make an editable table and then check the data to make sure its valid. Im not sure how to change the color of just one cell. I would like to get a cell, for

7条回答
  •  独厮守ぢ
    2020-11-30 14:43

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int col) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        int control = row;
        control = control % 2;
        control = (control == 0) ? 1 : 0;
        if (control == 1) {
            c.setBackground(Color.green);
        } else {
            c.setBackground(Color.cyan);
        }
        return c;
    }
    

提交回复
热议问题