title: How to set cell background and decimal format together during cell selection
body: <p>I am sorry if the title is not explanatory enough. </p>
  
  <p>I have written a small extension to DefaultTableRenderer class in Java in order to specify cellBackgourd, cellForeground, Alignment and decimal control. I using this class in matlab to control and customise JIDE tables. But have re-created the issue in Java, in order to increase chances of a reply or a possible workaround.</p>
  
  <p>I am successfully able to set cell Background, Foreground, alignment and decimal places as required when the table initialises and displays. However, as soon as I select a row/cell I loose decimal control on displayed data as shown in figure below. Please note that I am using specific cell selection colours, I guess, I am not implementing it properly.</p>
  
  <p><a href="https://i.stack.imgur.com/9BmJE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9BmJE.jpg" alt="enter image description here"></a> </p>
  
  <p>I think, the problem is one of last two lines:</p>
  
  <pre><code>return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>return cell;
  </code></pre>
  
  <p>Java code:</p>
  
  <pre><code>import java.awt.*;
  import java.text.DecimalFormat;
  import javax.swing.*;
  import javax.swing.table.*;
  
  public class DecimalPlacesInTable extends JFrame {
      public static void main( String[] args ) {
          DecimalPlacesInTable frame = new DecimalPlacesInTable();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.pack();
          frame.setVisible( true );
      }
  
      public DecimalPlacesInTable() {
          Object[] columnNames = { "A", "B", "C" };
          Object[][] data = {
                  {new Double( 850.503 ), new Double( 850.545 ), new Double( 80.54553 ) },
                  {new Double( 50.52503 ), new Double( 36.4554 ), new Double( 50.41453 ) },
                  {new Double( 80.544653 ), new Double( 8.3 ), new Double( 80.4553 ) },
                  {new Double( 50.1553 ), new Double( 246.0943 ), new Double( 50.455 ) }};
  
          JTable table = new JTable(data, columnNames);
  
          // Tell the table what to use to render our column of doubles
          for (int i=0; i&lt;3; i++) {
              table.getColumnModel().getColumn(i).setCellRenderer(new DecimalFormatRenderer());
              getContentPane().add(new JScrollPane(table));
          }
      }
  
      // Custom Renderer class
      static class DecimalFormatRenderer extends DefaultTableCellRenderer
      {
          public Component getTableCellRendererComponent
                  (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
              JComponent cell = (JComponent) super.getTableCellRendererComponent
                      (table, value, isSelected, hasFocus, row, column);
              // set color
              cell.setBackground(new Color(0xC8C8C8));
              cell.setForeground(new Color(0xFFFFFF));
  
              //set Alignment
              ((JLabel) cell).setHorizontalAlignment(SwingConstants.CENTER);
  
              //set selection colors
              if (isSelected) {
                  cell.setBackground(new Color(0x3399FF));
                  cell.setForeground(new Color(0x000000)); // AM
              } else {
                  // set decimals
                  DecimalFormat DecimalFormatter = new DecimalFormat("#.00");
                  value = DecimalFormatter.format(value);
                  return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
              }
              return cell;
          }
      }
  }
  </code></pre>