枚举的表格单元格渲染器
我有一个包含两列的表:属性和值! 属性是一个枚举。现在我为枚举类设置一个单元格渲染器(应以小写形式显示)。
问题是:表永远不会调用渲染器!
Enum(只是一个示例):
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(只是一个示例):
// 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());
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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要提供自己的
AbstractTableModel
实现,它实现getColumnClass(int c)
并返回列的类。背景:表实现不会尝试将每个单元格的值映射到渲染器,而是向模型询问整个列的类。
You need to supply your own implementation of
AbstractTableModel
which implementsgetColumnClass(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.