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

Can't set background in Box TableCellRenderer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • client-libs

      FULL PRODUCT VERSION :
      C:\Program Files\Java\jre1.5.0_06\bin>.\java -version
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP
      Service Pack 2

      A DESCRIPTION OF THE PROBLEM :
      When using the Box class for a table cell renderer, you cannot set the background color.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the code provided and select different rows in the table.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When selecting a row the entire row (both columns) should use the table selection color
      ACTUAL -
      The cells render with a Box are not rendered in the table selection color

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Component;

      import javax.swing.Box;
      import javax.swing.BoxLayout;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.table.AbstractTableModel;
      import javax.swing.table.TableCellRenderer;

      public class BoxSelector extends JFrame
      {
      {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      TableModel tableModel = new TableModel();
      Table table = new Table(tableModel);
      JScrollPane scrollPane = new JScrollPane(table);
      getContentPane().add(scrollPane);
      setBounds(200, 200, 200, 200);
      setVisible(true);
      }

      public static void main(String[] args)
      {
      new BoxSelector();
      }

      class Table extends JTable
      {

      public Table(TableModel tableModel)
      {
      super(tableModel);
      columnModel.getColumn(0).setCellRenderer(new BoxRenderer());
      columnModel.getColumn(1).setCellRenderer(new PanelRenderer());

      }
      class BoxRenderer implements TableCellRenderer
      {
      Box box = new Box(BoxLayout.X_AXIS);

      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
      {
      if (isSelected)
      box.setBackground(Table.this.getSelectionBackground());
      else
      box.setBackground(Table.this.getBackground());

      return box;
      }
      }

      class PanelRenderer implements TableCellRenderer
      {
      JPanel panel = new JPanel();

      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
      {
      if (isSelected)
      panel.setBackground(Table.this.getSelectionBackground());
      else
      panel.setBackground(Table.this.getBackground());

      return panel;
      }
      }
      }

      class TableModel extends AbstractTableModel
      {

      public int getColumnCount()
      {
      return 2;
      }

      public int getRowCount()
      {
      // TODO Auto-generated method stub
      return 2;
      }

      public Object getValueAt(int rowIndex, int columnIndex)
      {
      return null;
      }

      public String getColumnName(int index)
      {
      switch (index)
      {
      case 0: return "Box";
      case 1: return "JPanel";
      default: return null;
      }
      }
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use the JPanel instead of Box

            shickeysunw Shannon Hickey (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: