title: Swing JTable - Change background color of a row in function of the previous row
body: <p>I have a swing JXTable and the requirement is that background color must change if a field differs from the previous row.</p>
  
  <p>Here is an example : </p>
  
  <ul>
  <li><p>Row 0 : var = 1 -> Background BLUE</p></li>
  <li><p>Row 1 : var = 1 -> Background BLUE</p></li>
  <li><p>Row 2 : var = 2 -> Background RED</p></li>
  <li><p>Row 3 : var = 3 -> Background BLUE</p></li>
  </ul>
  
  <p>I've tried several solutions.</p>
  
  <p>Overriding prepareRenderer, but this solution is not working fine. The lines blinks and color changes in function of the first line visible in the table.</p>
  
  <pre><code>    @Override
  public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
  {
      Component c = super.prepareRenderer ( renderer , row , column );
      if(lastRow  != -1)
      {
          Data actualData = ( ( MessagesTableModel ) getModel ( ) ).getData ( row );
          Data previousData = ( ( MessagesTableModel ) getModel ( ) ).getData ( lastRow );
          if ( previousData != null )
          {
              if ( previousData.getNbr ( ) != actualData.getNbr ( ) )
              {
                  if(lastColor.equals ( COLOR1 ))
                  {
                      System.out.println ("A");
                      lastColor = COLOR2;
                  }
                  else
                  {
                      System.out.println ("b");
                      lastColor = COLOR1;
                  }
              }
          }
      }
      lastRow = row;
      c.setBackground ( lastColor );
      return c;
  }
  </code></pre>
  
  <p>I've also think of defining a custom TableCellRender, but it was not concluant.</p>
  
  <p>So I'm stuck I don't know how to do.
  Do you have any suggestions ?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've tried with HighlightPredicate since I'm using JXTable</p>
  
  <pre><code>        HighlightPredicate predicate = new HighlightPredicate ( )
      {
          public boolean isHighlighted(Component renderer, ComponentAdapter adapter)
          {
              Data actualData = ( ( MessagesTableModel ) getModel ( ) ).getData ( adapter.row );
              if ( adapter.row - 1 &gt;= 0 )
              {
                  Data previousData = ( ( MessagesTableModel ) getModel ( ) )
                          .getData ( adapter.row - 1 );
                  if ( actualData.getNbr ( ) != previousData.getNbr ( ) )
                  {   
                      return true;
                  }
  
              }
              return false;
          }
      };
  
      addHighlighter ( new ColorHighlighter ( predicate , COLOR1 , null ) );
  </code></pre>
  
  <p>This time, the color changes if the value change, but if the row right after is not changing, the color changes. </p>
  
  <ul>
  <li><p>Row 0 : var = 1 -> Background BLUE</p></li>
  <li><p>Row 1 : var = 1 -> Background BLUE</p></li>
  <li><p>Row 2 : var = 2 -> Background RED</p></li>
  <li><p>Row 3 : var = 2 -> Background BLUE</p></li>
  </ul>
  
  <p>This is normal because that's what is coded :)
  But I'm in front of the same problem, how to know if the previous row was highlighted or not.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I've found a solution, don't seem very clean but it works.</p>
  
  <pre><code>        HighlightPredicate predicate = new HighlightPredicate ( )
      {
          public boolean isHighlighted(Component renderer, ComponentAdapter adapter)
          {
              Data actualData = ( ( MessagesTableModel ) getModel ( ) ).getData ( adapter.row );
              if ( adapter.row - 1 &gt;= 0 )
              {
                  Data previousData = ( ( MessagesTableModel ) getModel ( ) )
                          .getData ( adapter.row - 1 );
                  if ( actualData.getMsgNbr ( ) != previousData.getMsgNbr ( ) )
                  {   
                      adapter.row--;
                      if(isHighlighted ( renderer , adapter ))
                      {
                          return false;
                      }
                      else
                      {
                          return true;
                      }                       
                  }
                  else
                  {
                      adapter.row--;
                      if(isHighlighted ( renderer , adapter ))
                      {
                          return true;
                      }
                      else
                      {
                          return false;
                      }
                  }
              }
              return false;
          }
      };
  
      addHighlighter ( new ColorHighlighter ( predicate , COLOR1 , null ) );
  </code></pre>
  
  <p>Thank you all for your help !</p>