title: Java JTable sort with TableCellRenderer
body: <p>I'm somewhat new to Java and I'm having problems with <code>JTable</code> with sort and <code>TableCellRenderer</code>.</p>
  
  <p>I have a table with 15 columns populated with values where on some columns I use the <code>TableCellRenderer</code> to change the color of foreground to green or red acording the values on the cell.</p>
  
  <p>Everything is working perfectly until I try to sort by some column (the sorting part is ok)...
  The problem is that the formating colors don't reflect the change made by the sort operation. The colors stay unchanged at their original position on the table before the sort. Is there a simple way to overcome this problem?</p>
  
  <p>I searched, but I did not find a solution I can implement. </p>
  
  <p>TIA</p>
  
  <p>JL</p>
  
  <p>EDIT: Related source code posted (not complete implementation)</p>
  
  <pre><code>private void buildTab(){
      myIUserInterface.removeBottomTab("just a test");
      myJPanel = myIUserInterface.getBottomTab("just a test");
      myJScollPane = new JScrollPane();
      myTable = new JTable();
      myTable.setAutoCreateRowSorter(true);
      myTableContent = myFillTableData();
      String[] tableHeaders = {
              "X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15"
          };
      myTable.setModel(new DefaultTableModel(myTableContent, tableHeaders) {
  
          @Override
          public boolean isCellEditable(int rowIndex, int columnIndex) {
              return false;
          }
      });
      MyChangeCellColor myCCC = new MyChangeCellColor();
      Enumeration&lt;TableColumn&gt; allColumns = myTable.getColumnModel().getColumns();
      while(allColumns.hasMoreElements()){
          TableColumn column = allColumns.nextElement();
          column.setCellRenderer(myCCC);
      }
      myJScollPane.setViewportView(myTable);
  
      GroupLayout myLayout = new GroupLayout(myJPanel);
      myJPanel.setLayout(myLayout);
      myLayout.setHorizontalGroup(
          myLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(myJScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 890, Short.MAX_VALUE)
      );
      myLayout.setVerticalGroup(
          myLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(myJScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 672, Short.MAX_VALUE)
      );
  }
  
  
  class MyChangeCellColor extends JLabel implements TableCellRenderer {
      @Override
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
              boolean hasFocus, int row, int column) {
          setHorizontalAlignment(CENTER);
          setOpaque(true);
          setBackground(Color.WHITE);
          if (column == 0){
              setForeground(Color.BLACK);
          }
          if (column == 1){
              setForeground(Color.BLACK);
          }
          if (column == 2){
              setForeground(Color.BLACK);
          }
          if (column == 3){
              setForeground(Color.BLACK);
          }
          if (column == 4){
              if ("LONG".equals(myTable.getModel().getValueAt(row, 4))) {
                  setForeground(Color.GREEN.darker());
              }
              if ("SHORT".equals(myTable.getModel().getValueAt(row, 4))) {
                  setForeground(Color.RED);
              }
          }
          if (column == 5){
              setForeground(Color.BLACK);
          }
          if (column == 6){
              setForeground(Color.BLACK);
          }
          if (column == 7){
              setForeground(Color.BLACK);
          }
          if (column == 8){
              setForeground(Color.BLACK);
          }
          if (column == 9){
              setForeground(Color.BLACK);
          }
          if (column == 10){
              setForeground(Color.BLACK);
          }
          if (column == 11){
              double d = Double.valueOf(myTable.getModel().getValueAt(row, 11).toString());
              if (d &gt; 0) {
                  setForeground(Color.GREEN.darker());
              }
              if (d == 0) {
                  setForeground(Color.BLACK);
              }
              if (d &lt; 0) {
                  setForeground(Color.RED);
              }
          }
          if (column == 12){
              double d = Double.valueOf(myTable.getModel().getValueAt(row, 12).toString());
              if (d &gt; 0) {
                  setForeground(Color.GREEN.darker());
              }
              if (d == 0) {
                  setForeground(Color.BLACK);
              }
              if (d &lt; 0) {
                  setForeground(Color.RED);
              }
          }
          if (column == 13){
              setForeground(Color.BLACK);
          }
          if (column == 14){
              setForeground(Color.BLACK);
          }
          setText(value.toString());
          return this;
      }
  }
  </code></pre>
  
  <p>EDIT 2: Solution fond and pasted for reference if anyone needs it! Just need to reference the model rows on the rows returned by JTable on TableCellRenderer class (not sure if it's correctly explained anyway you can see the changes below):</p>
  
  <pre><code>class MyChangeCellColor extends JLabel implements TableCellRenderer {
      @Override
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
              boolean hasFocus, int row, int column) {
          setHorizontalAlignment(CENTER);
          setOpaque(true);
          setBackground(Color.WHITE);
          if (column == 0){
              setForeground(Color.BLACK);
          }
          if (column == 1){
              setForeground(Color.BLACK);
          }
          if (column == 2){
              setForeground(Color.BLACK);
          }
          if (column == 3){
              setForeground(Color.BLACK);
          }
          if (column == 4){
              if ("LONG".equals(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 4))) {
                  setForeground(Color.GREEN.darker());
              }
              if ("SHORT".equals(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 4))) {
                  setForeground(Color.RED);
              }
          }
          if (column == 5){
              setForeground(Color.BLACK);
          }
          if (column == 6){
              setForeground(Color.BLACK);
          }
          if (column == 7){
              setForeground(Color.BLACK);
          }
          if (column == 8){
              setForeground(Color.BLACK);
          }
          if (column == 9){
              setForeground(Color.BLACK);
          }
          if (column == 10){
              setForeground(Color.BLACK);
          }
          if (column == 11){
              double d = Double.valueOf(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 11).toString());
              if (d &gt; 0) {
                  setForeground(Color.GREEN.darker());
              }
              if (d == 0) {
                  setForeground(Color.BLACK);
              }
              if (d &lt; 0) {
                  setForeground(Color.RED);
              }
          }
          if (column == 12){
              double d = Double.valueOf(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 12).toString());
              if (d &gt; 0) {
                  setForeground(Color.GREEN.darker());
              }
              if (d == 0) {
                  setForeground(Color.BLACK);
              }
              if (d &lt; 0) {
                  setForeground(Color.RED);
              }
          }
          if (column == 13){
              setForeground(Color.BLACK);
          }
          if (column == 14){
              setForeground(Color.BLACK);
          }
          setText(value.toString());
          return this;
      }
  }
  </code></pre>