samedi 6 mars 2021

Change Background color using JCheckBox in JTable

I have a JCheckBox that runs a JTable in some columns. But when I set the color in the cells of that column, it seems to overwrite the rendered rendering object (JCheckBox).

The following code snippet is what I'm trying to do:

public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
public CheckBoxRenderer() {
    JCheckBox checkBox = new JCheckBox();
    setHorizontalAlignment(JLabel.CENTER);
    setBorderPainted(true);
        setOpaque(true);
    checkBox.setHorizontalAlignment(SwingConstants.CENTER);
    checkBox.setBackground(Color.blue);    }
public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);        
    if (isSelected) {
        setForeground(table.getSelectionForeground());
        super.setBackground(table.getSelectionBackground());
        setBackground(table.getSelectionBackground());
        return c;        } else {
        table.setShowGrid(true);
        table.setGridColor(Color.blue);
        setBackground(Color.RED);
        setForeground(table.getForeground());
        setBackground(table.getBackground());        }
    setSelected(value != null && (Boolean.valueOf(value.toString()).booleanValue()));
    if (row%2 == 0){
                c.setBackground(Color.WHITE);
            }else {
                c.setBackground(Color.LIGHT_GRAY);                }  return this;    }}

The problem is that I want the background color of the unchecked checkboxes to be red, but this is not done




Aucun commentaire:

Enregistrer un commentaire