2

我有一个包含两列的表:属性和值!属性是一个枚举。现在我为枚举类设置了一个单元格渲染器(应该以小写形式显示)。

问题是:表永远不会调用渲染器!

枚举(只是一个例子):

public enum Attribute {
  BLUE,BLACK,RED;
}

单元格渲染器:

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 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());
4

1 回答 1

4

您需要提供自己的实现,AbstractTableModel哪些实现getColumnClass(int c)并返回列的类。

背景:表格实现不会尝试将每个单元格的值映射到渲染器,而是向模型询问整个列的类。

于 2012-02-15T11:16:32.103 回答