我用它DefaultComboBoxModel
来显示一个客户列表JComboBox
.该列表目前仅显示其名称.我还想在其中引用每个客户,DefaultComboBoxModel
以便在选择名称时,它还包含对真实客户对象的引用.
为了实现这一点,我怀疑我必须扩展DefaultComboBoxModel
并可能覆盖该addElement()
方法?或者我可以添加一个新方法,也可以存储我对客户的引用?如果是这样,我是否必须查看源代码DefaultComboBoxModel
以了解它如何存储元素?对不起,如果这个问题令人困惑,但我无法弄清楚如何以正确的方式做到这一点.谢谢阅读.
If you in your Customer object override toString() to return whatever you want presented in the JComboBox it will work just fine. If you are using toString for other purposes you need to override the model or renderer to use the correct fields from the Customer object.
- 更新Tom的建议 - 创建一个新对象CustomerView,它包装真实的客户对象,因此可以提供对它的引用,但也会覆盖toString()以返回客户的名称.
归档时间: |
|
查看次数: |
4174 次 |
最近记录: |