非常简单的 JCombobox 单元格编辑器问题
Very simple JCombobox cell editor issue
我被一些我确信非常简单的事情难住了。
我有一个带有对象的 Jcombobox 单元格编辑器,比方说 'Customer'。
客户有一个 'code' 和一个 'description'。
客户的 toString 已被覆盖为 return 'code'.
我在 table 中有两个单元格,想要一个列出代码,一个列出描述。两个组合框列表都填充了 Customer 对象,两者都是 return CODE。如何让第二个组合框显示 DESCRIPTION?
我试图创建一个新的子 class 来覆盖 toString 方法并用它填充第二个组合框但是为了将值加载到新的子类型中我必须重新抓取数据并创建新的子 classes 或在 'customer' 对象上实现克隆方法,每次添加新变量时我都必须更新它。
是否有覆盖 jcombobox 渲染器的简单方法?
谢谢
在 toString 方法中加入条件语句是否可行?如果可能的话,这似乎比覆盖 JComboBox 更容易。
Is there a simple way of overriding the jcombobox renderer?
您需要创建自己的自定义呈现器来呈现特定的 属性 客户对象。
渲染器的一般形式为:
class FooRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Foo)
{
Foo foo = (Foo)value;
setText( foo.getDescription() );
}
return this;
}
}
查看 Combo Box With Custom Renderer 了解更多信息和更完整的实施。
我被一些我确信非常简单的事情难住了。
我有一个带有对象的 Jcombobox 单元格编辑器,比方说 'Customer'。 客户有一个 'code' 和一个 'description'。 客户的 toString 已被覆盖为 return 'code'.
我在 table 中有两个单元格,想要一个列出代码,一个列出描述。两个组合框列表都填充了 Customer 对象,两者都是 return CODE。如何让第二个组合框显示 DESCRIPTION?
我试图创建一个新的子 class 来覆盖 toString 方法并用它填充第二个组合框但是为了将值加载到新的子类型中我必须重新抓取数据并创建新的子 classes 或在 'customer' 对象上实现克隆方法,每次添加新变量时我都必须更新它。
是否有覆盖 jcombobox 渲染器的简单方法?
谢谢
在 toString 方法中加入条件语句是否可行?如果可能的话,这似乎比覆盖 JComboBox 更容易。
Is there a simple way of overriding the jcombobox renderer?
您需要创建自己的自定义呈现器来呈现特定的 属性 客户对象。
渲染器的一般形式为:
class FooRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Foo)
{
Foo foo = (Foo)value;
setText( foo.getDescription() );
}
return this;
}
}
查看 Combo Box With Custom Renderer 了解更多信息和更完整的实施。