【发布时间】:2011-08-05 02:14:31
【问题描述】:
我有这个类来表示组合框中的选择:
class Choice(object):
def __init__(self, id, label):
self.id = id
self.label = label
def toString(self):
print "in Choice.toString" #for debugging
return self.label
我有一个 Choice 对象数组,我想在 JComboBox 中显示标签值,但以后可以在数组超出范围后检索 id。
关于 JComboBox 渲染器的主题,the Java Swing tutorial says,
默认渲染器知道如何渲染字符串和图标。如果将其他对象放在组合框中,默认渲染器会调用 toString 方法提供要显示的字符串。
所以,鉴于我已经在我的 Choice 类中添加了一个 toString() 方法,我应该能够做到这一点:
choices = [Choice(1, 'foo'), Choice(3, 'bar'), Choice(5, 'baz')]
combo = JComboBox(choices)
然后:
pickedId = combo.getSelectedItem().id
但是,我的组合中显示的文本类似于 <command.Choice object at 0x2>,而我放入 Choice.toString() 的 print 语句从未发生过。
有什么想法吗?
【问题讨论】:
-
我不熟悉 python/jython,但您创建的 toString() 方法可能没有扩展“void Object.toString()”(可能签名不匹配)。尝试使用 javap 检查编译的类。
标签: java python swing jython tostring