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

DefaultTableCellRenderer has new optimisation that make it difficult to subclass

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • client-libs
    • beta
    • generic
    • generic



      Name: yyT116575 Date: 10/25/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      If you wish to change the background color for a cell dependent on value,
      selection, table, focus etc. Then overriding setValue() in
      DefaultTableCellRenderer has not got the parameters you require.

      So, override getTableCellRendererComponent instead :) !

      The following code seems reasonable :-

      Component ret = super.getTableCellRendererComponent(table,value, isSelected,
      hasFocus, row, column );

      if( ((Integer)value).intValue() == 0 ){
          ret.setForeground( Color.yellow );
          ret.setBackground( Color.red );
      }else {
          if( isSelected ){
              ret.setBackground(table.getSelectionBackground());
              ret.setForeground(table.getSelectionForeground());
          }
          else{
              ret.setBackground(table.getBackground());
              ret.setForeground(table.getForeground());
          }
      }
      return ret;

      This works fine 1.1.8 to 1.2.2, trouble is, the new optimisation in 1.3 calls
      setOpaque( false ) in getTableCellRendererComponent, based on the current
      colors, which you were just about to change!

      The result is VERY confusing and took me hours to find!
      (Review ID: 110756)
      ======================================================================

      Name: krC82822 Date: 11/01/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      DefaultTableCellRenderer has a new, undocumented optimization that is confusing
      developers. The getTableCellRendererComponent() method will avoid painting the
      cell background by setting opaque to false if the cell background matches the
      table background. While this speeds up painting, it is confusing to subclassers
      because they don't know that this is happening.

      Here is the optimization, taken from the code for DefaultTableCellRender.java:

      // ---- begin optimization to avoid painting background ----
      Color back = getBackground();
      boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) &&
      table.isOpaque();
      setOpaque(!colorMatch);
      // ---- end optimization to aviod painting background ----

      Subclassers often override this class to specify a background color, then get
      confused because it doesn't paint properly. To see why, read the comments to bug
      4336152.

      The documentation for this method needs to describe this optimization.
      (Review ID: 109220)
      ======================================================================

            pmilnesunw Philip Milne (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: