0

I have a jTable as following :

enter image description here

I want when the value in the Quantité is less than the value in the Min seuil de suantité, to change the color of the row to pink.

In the load of the program all works fine, but when I do some event like click on the table, the color of all the rows is changed even if the the value of the Quantité is not less than the value of the Min seuil de quantité :

enter image description here

this is my cell rendering :

public class CustomTableCellRenderer extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table,
            Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
        Component cell = super.getTableCellRendererComponent(
                table, obj, isSelected, hasFocus, row, column);

        setHorizontalAlignment(SwingConstants.LEFT);

        int selectedRow = table.convertRowIndexToModel(row);
        if (table.getModel().getValueAt(selectedRow, 3) != null && table.getModel().getValueAt(selectedRow, 4) != null) {
            int quantite = Integer.parseInt(table.getModel().getValueAt(selectedRow, 3).toString());
            int minQuantite = Integer.parseInt(table.getModel().getValueAt(selectedRow, 4).toString());
            if (quantite < minQuantite) {
                if (isSelected) {
                    cell.setBackground(new Color(255, 138, 239));
                } else {
                    cell.setBackground(new Color(252, 189, 252));
                }
            }
        }
        return cell;
    }
}

and this is the code which allows me to affect the cell rendering to my table :

private void cellRendering(){
        for (int i = 0; i < masterTable.getColumnCount(); i++) {
            tcol = masterTable.getColumnModel().getColumn(i);
            tcol.setCellRenderer(new CustomTableCellRenderer());
        }
    }
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Aimad Majdou
  • 563
  • 4
  • 13
  • 22

1 Answers1

1

The renderer is a rubber stamp that remembers what ink was applied last. Be sure to set the desired color each time the renderer is invoked. More details can be found here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    @user2417302 not wrong, again the same issue, really time to use prepareRenderer, do not parse whatever stored in XxxTableModel, put there Double, Integer, whatever from Number instance, most of issue will be resolved by posting an SSCCE, short, runnable, compilable, with hardcoded value for XxxTableModel – mKorbel Jun 03 '13 at 10:00
  • @user2417302 for rest of (any) issues to [see my post about](http://stackoverflow.com/q/16814512/714968), I'm sure that I saw very similair question on another Java Forum or am wrong – mKorbel Jun 03 '13 at 10:05