Search code examples
javaswingcolorstablecellrenderer

Changing colors in cells when I change the cell of column 5, he doesn't stop here, he change the next cell


I have a problem with changing colors in cells, when I change the cell of column 5, he doesn't stop here, he change the next cell... Here is my code :

public class MyRenderer extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        Etudiant E = new Etudiant();

        if (column == 0) {
            int id = E.getAIdEtud(table.getModel().getValueAt(row, 1).toString(), table.getModel().getValueAt(row, 2).toString());
            if (E.IsRoudoublan(id) && E.IsExcclu(id)) {
                c.setBackground(new java.awt.Color(0, 0, 255));
            } else if (E.IsExcclu(id)) {
                c.setBackground(new java.awt.Color(255, 0, 0));
            } else if (E.IsRoudoublan(id)) {
                c.setBackground(new java.awt.Color(20, 200, 0));
            }
        } else if (column == 5) {
            if (Integer.parseInt(table.getModel().getValueAt(row, 5).toString()) >= 3) {
                c.setBackground(new java.awt.Color(20, 200, 20));
            }
        } else {
            c.setBackground(new java.awt.Color(100, 100, 100));
        }

        return c;

    }
}

Solution

  • I took your snippet and corrected all problems that I found. Check for all lines with comments.

    Notes:

    • Provide a color for all possible code paths, you forgot to provide a color in two spots (see the // << provide ELSE part here, eg: lines)
    • Indexes reported in the TableCellRenderer.getTableCellRendererComponent method are view indexes.
    • When indexing the model, you need to use model indexes. You are using view indexes to index the model. That can lead to problems e.g. when your table is sorted, when you rearrange columns or when you apply a row filter. Look for the lines with comments at the end for corrections.

    public class MyRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowViewId, int columnViewId) {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, rowViewId, columnViewId);
            Etudiant E = new Etudiant();
    
            int rowModelId = table.convertRowIndexToModel(rowViewId); // << for lookup of values in the model
            int colModelId = table.convertColumnIndexToModel(columnViewId); // << for lookup of values in the model
    
            if (colModelId == 0) { // << you mean to check if the model index is 0 here
                int id = E.getAIdEtud(table.getModel().getValueAt(rowModelId, 1).toString(), table.getModel().getValueAt(rowModelId, 2).toString()); // << when indexing the model, use model indexes
                if (E.IsRoudoublan(id) && E.IsExcclu(id)) {
                    c.setBackground(new java.awt.Color(0, 0, 255));
                } else if (E.IsExcclu(id)) {
                    c.setBackground(new java.awt.Color(255, 0, 0));
                } else if (E.IsRoudoublan(id)) {
                    c.setBackground(new java.awt.Color(20, 200, 0));
                }
                // << provide ELSE part here, eg:
                else {
                    c.setBackground(new java.awt.Color(100, 100, 100));
                }
            } else if (colModelId == 5) { // << you mean to check if the model index is 5 here
                if (Integer.parseInt(table.getModel().getValueAt(rowModelId, 5).toString()) >= 3) { // << when indexing the model, use model indexes
                    c.setBackground(new java.awt.Color(20, 200, 20));
                }
                // << provide ELSE part here, eg:
                else {
                    c.setBackground(new java.awt.Color(100, 100, 100));
                }
            } else {
                c.setBackground(new java.awt.Color(100, 100, 100));
            }
    
            return c;
        }
    }