Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6723524

Invalid background of components displayed in JTable in Nimbus L&F

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_10-beta"
      Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b25)
      Java HotSpot(TM) Client VM (build 11.0-b12, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      6.0.6001

      A DESCRIPTION OF THE PROBLEM :
      The cell background is invalid, when I want to display a JCheckBox or JComboBox in a JTable:

      JCheckBox centeredCheckBox = new JCheckBox();
      centeredCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
      myTable.getTableHeader().getColumnModel().getColumn(0).setCellEditor(
                      new DefaultCellEditor(centeredCheckBox));
      myTable.getTableHeader().getColumnModel().getColumn(0).setCellRenderer(
                      new CheckBoxCellRenderer());


      The CheckBoxCellRenderer is defined as follows:


      public class CheckBoxCellRenderer extends JCheckBox implements TableCellRenderer {

      public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {

      setOpaque(true);
      if (isSelected) {
      setBackground(table.getSelectionBackground());
      } else {
      setBackground(table.getBackground());
      }
                     
      this.setHorizontalAlignment(SwingConstants.CENTER);
      setSelected((Boolean) value);
      return this;
      }
      }
              

      First, setting opaque to false makes the background ALWAYS white and ignores the current selection - just as if the JTable forgot to paint the background there.

      However, the table.getBackground() seems to return invalid values - I would expect it to return the color of the currently rendered row, and it does not - it returns sometimes dark gray, which isn't any of the background colors of the nimbus L&F. This causes I cannot write a universal CellRenderer that works good for all L&Fs.

      As a workaround I hardcoded the 2 Nimbus background colors in the renderer, but I'm not happy with this solution.

      The problem doesn't exist in the MetalLookAndFeel, regardless opaque is set or not.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {

      setOpaque(true);
      if (isSelected) {
      setBackground(table.getSelectionBackground());
      } else {
      if (row % 2 == 1)
      setBackground(Color.WHITE);
      else
      setBackground(table.getBackground());
      }

      //...
      }

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: