title: JTable row color change based on a column value- on pop up click
body: <p>My <code>jTable</code> is loaded with data and this is where I call my Pop up functionality on <code>jTable</code>. </p>
  
  <pre><code>jTable.addMouseListener(new TablePopupListener(jTable));
  displayTable();
  </code></pre>
  
  <p>So basically, if I right-click a row, a popup(credit check) comes up and if I click it is setting a value to the last cell in that row. Now, based on this column cell value I have to define the color of a row. Let's say if the cell value fails then turn the row to red else to green. I have tried <code>customCellRenderer</code> and defined my condition but there is no change in row color. The custom cell renderer worked great for a button functionality that I had to write, though. The below code uses prepare <code>cellRenderer</code> which I felt is easy but I don't see any change in row color.</p>
  
  <p>I am missing some connection, plz provide me help.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>    class TablePopupListener extends MouseAdapter implements ActionListener { 
          JPopupMenu popup; 
          JTable table; 
          int[] selRows; 
          TableModel model; 
          ArrayList rowValueList = new ArrayList(); 
          JMenuItem creditCheck = new JMenuItem("Credit Check");
  
          public TablePopupListener(JTable jTable) {
              this.table = jTable;
              model = table.getModel();
              popup = new JPopupMenu();
              JMenuItem creditCheck = new JMenuItem("Credit Check");
              creditCheck.addActionListener(this);
              popup.add(creditCheck);
  
          }
  
          public void mousePressed(MouseEvent me) {
             firePopup(me);
          }
  
          public void mouseReleased(MouseEvent me) {
             firePopup(me);
          }
  
          public void firePopup(MouseEvent me) {
  
              /*
               * The popup menu will be shown only if there is a row selection in the
               * table
               */
  
              // popup.show(table, me.getX(), me.getY());
              if (me.isPopupTrigger() &amp;&amp; table.getModel().getRowCount() != 0
                 &amp;&amp; table.getSelectedRow() != -1) {
                // popup.show(table,me.getX(),me.getY());
                if (me.isPopupTrigger()) {
                     JTable source = (JTable) me.getSource();
                     int row = source.rowAtPoint(me.getPoint());
                     int column = source.columnAtPoint(me.getPoint());
  
                     if (!source.isRowSelected(row))
                        source.changeSelection(row, column, false, false);
  
                     popup.show(table, me.getX(), me.getY());
  
                }
              }
           }
  
      public void actionPerformed(ActionEvent ae) {
          if (ae.getActionCommand().equals("Credit Check")) {
              System.out.println("you have clicked creditCheckpopup");
              selRows = table.getSelectedRows();
              if (selRows.length &gt; 0) {
                  for (int i = 0; i &lt; selRows.length; i++) {
                    // get Table data
                      for (int j = 1; j &lt; (table.getColumnCount()) - 1; j++) {
                          rowValueList.add(model.getValueAt(selRows[i], j));
                      }
                      System.out.println("Selection : " + rowValueList);
                  }
              } else {
                  System.out.println("you have clicked something idiot");
              }
  
              int result = new COpxDeal(rowValueList).CheckCredit();
              if (result == 1)
                  rowValueList.add("pass");
              else
                  rowValueList.add("fail");
              String aValue = (String) rowValueList.get(14);
              for (int i = 0; i &lt; selRows.length; i++) {
                  model.setValueAt(aValue, selRows[i], 15);
              }
  
       // inserted comment (Kleopatra): where are we? that's outside of the TablePopup?
       // okay, nothing like copying the code into an IDE and let that do the formatting, silly me ;-)
       // this is indeed _inside_ the popup, that is the table is recreated
              table = new JTable(model) {
                  public Component prepareRenderer(TableCellRenderer renderer,
                          int row, int column) {
                      Component c = super.prepareRenderer(renderer, row, column);
                      JComponent jc = (JComponent) c;
  
                      // if (!isRowSelected(row)){
                      // c.setBackground(getBackground());
                      // System.out.println(isRowSelected(row));
                      // }
                      int modelRow = convertRowIndexToModel(row);
                      String strTestValue = "fail";
                      String strTblValue = (String) getModel().getValueAt(
                              modelRow, 15);
                      System.out.println("result :" + strTblValue);
                      if (strTblValue == null || strTblValue.equals(""))
                          System.out.println("there is nothing in strTblValue");
                      else if (strTestValue.equals(strTblValue)) {
                          jc.setBackground(Color.RED);
                      } else {
                          jc.setBackground(Color.green);
                      }
  
                      return c;
                  }
              };
  
          }
  
      }
  }
  </code></pre>