2

I have a table with two columns: attribute and value! Attribute is an enum. Now I set a cell renderer for the enum class (should be displayed in lowercase).

The problem is: the table never call the renderer!

Enum (just an example):

public enum Attribute {
  BLUE,BLACK,RED;
}

Cell Renderer:

public class AttributeTableCellRenderer
    extends
        AbstractTableCellRenderer<Attribute> {  
    @Override
    protected Object getText(Attribute attribute) {
        System.out.println("call");
        if (null == attribute) {
            return null;
        }
        return attribute.toLowerCase();
    }
}

Table (just an example):

// table model
Vector<Object> v;
Vector<String> header = new Vector<String>(Arrays.asList("attribute", "values"));
Vector<Vector<?>> data = new Vector<Vector<?>>();
// fill with data
for (final Attribute attribute : Attribute.values()) {
  v = new Vector<Object>();
  v.add(attribute);
  v.add("blah");
  data.add(v);
}
//table
TableModel tm = new DefaultTableModel(data, header);
JTable table = new JTable(tm);
table.setDefaultRenderer(String.class, new DefaultTableCellRenderer());
table.setDefaultRenderer(Attribute.class, new AttributeTableCellRenderer());
// will work
//table.setDefaultRenderer(Object.class, new AttributeTableCellRenderer());
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Marcel Jaeschke
  • 707
  • 7
  • 24

1 Answers1

4

You need to supply your own implementation of AbstractTableModel which implements getColumnClass(int c) and returns the class of the column.

Background: The table implementation doesn't try to map the value of each cell to a renderer but instead it asks the model for the class of the whole column.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820