Java JComboBoxes - 使用 setModel 访问对象(在 Swing 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3926543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JComboBoxes - Using setModel to access Objects (in Swing)
提问by Draineh
Quick background, I am using Netbeans to develop this (I don't have much experience with Swing and have lost points on experience at the chance to gain development speed). In Netbeans it is obviously restrictive as to what code you can modify to stop novice users breaking the code (which I have already amusingly done once) Anyway, I have a class of Objects, these Objects have a name property. Within the application I have directly initialised an array of these objects and called them "things";
快速背景,我正在使用 Netbeans 来开发它(我对 Swing 没有太多经验,并且在获得开发速度的机会上失去了经验点)。在 Netbeans 中,对于可以修改哪些代码来阻止新手用户破坏代码(我已经有趣地做过一次),显然是有限制的。无论如何,我有一类对象,这些对象有一个 name 属性。在应用程序中,我直接初始化了这些对象的数组并将它们称为“事物”;
Objects[] things = new Objects[2];
things[0] = new Objects("The first thing");
things[1] = new Objects("The second thing");
The contents and names are deliberately inane as this is a test to get this working (rather than pulling apart a part written program). After some research and reading I have discovered that I "should" be able to load objects into the setModel parameter using the following code;
内容和名称是故意空洞的,因为这是一个让这个工作正常的测试(而不是拆开一个部分编写的程序)。经过一些研究和阅读,我发现我“应该”能够使用以下代码将对象加载到 setModel 参数中;
new javax.swing.DefaultComboBoxModel(things[].name)
//The above is the code to use within setModel, the below is the completed example
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(things[].name));
This hasn't worked, and despite my best efforts to google this seems to be too specific to nail down a decent answer. The end result is that I would like to have; "The first thing" and "The second thing" displayed within the drop down list, and then obviously I can expand on this within the real program by referencing any other data held in that object on the screen.
这没有奏效,尽管我尽最大努力在谷歌上搜索,但这似乎太具体了,无法确定一个体面的答案。最终的结果是我想要的;“第一件事”和“第二件事”显示在下拉列表中,然后显然我可以通过引用屏幕上该对象中保存的任何其他数据在实际程序中对此进行扩展。
Any suggestions or even pointers to help me think this out would be appreciated.
任何帮助我思考这一点的建议甚至指示将不胜感激。
采纳答案by willcodejavaforfood
First of all, the constructor of the DefaultComboBoxModel can take an array, but the property name does not exist in an array so you cannot do that. You would have to modify your objects or the combobox to show the correct property of your object.
首先, DefaultComboBoxModel 的构造函数可以接受一个数组,但是数组中不存在属性名称,因此您不能这样做。您必须修改对象或组合框以显示对象的正确属性。
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(things[]));
You have several options :)
你有几个选择:)
- The quick and easy override toString to return name (assuming Objects is your class)
- Create a wrapper class (ObjectsWrapper) that in its toString() method returns Objects name
- Modify the JComboBox in some way, either the model or the renderer to show the desired property
- 快速轻松地覆盖 toString 以返回名称(假设 Objects 是您的类)
- 创建一个包装类 (ObjectsWrapper),在其 toString() 方法中返回对象名称
- 以某种方式修改 JComboBox,无论是模型还是渲染器以显示所需的属性
回答by zigdon
Wouldn't just implementing a toString()
on your objects to return their .name
property work with the default Combo Box model?
不只是toString()
在您的对象上实现 a来返回它们的.name
属性与默认的组合框模型一起工作吗?
See the similar question: Java Swing: Extend DefaultComboBoxModel and override methods
看到类似的问题:Java Swing: Extend DefaultComboBoxModel and override methods