JDK-6405078 : Can't set background in Box TableCellRenderer
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-03-28
  • Updated: 2011-02-16
  • Resolved: 2006-03-28
Related Reports
Duplicate :  
Description
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

Comments
EVALUATION There's two issues here. First, the Box that is being used is non-opaque, and therefore isn't supposed to paint its background. Setting the opacity to true should fix the problem. However, there was another bug, 4907674, such that opaque boxes were not filling their backgrounds. This has been fixed for Mustang. As such, running this test under Mustang with an opaque box works perfectly.
28-03-2006