如何按值设置选定的索引JComboBox
我想通过值而不是索引在 JComboBox 中设置选定的索引。怎么做呢?示例
public class ComboItem {
private String value;
private String label;
public ComboItem(String value, String label) {
this.value = value;
this.label = label;
}
public String getValue() {
return this.value;
}
public String getLabel() {
return this.label;
}
@Override
public String toString() {
return label;
}
}
JComboBox test = new JComboBox();
test.addItem(new ComboItem(0, "orange"));
test.addItem(new ComboItem(1, "pear"));
test.addItem(new ComboItem(2, "apple"));
test.addItem(new ComboItem(3, "banana"));
test.setSelectedItem("banana");
好的,我稍微修改了我的问题。我忘记了我的 JComboBox 中有一个自定义项目,这使得它变得更加困难。我无法执行 setSelectedItem,因为每个项目内都有一个 ComboItem。那么,我该如何完成这件事呢?
I want to set the selected index in a JComboBox by the value not the index. How to do that? Example
public class ComboItem {
private String value;
private String label;
public ComboItem(String value, String label) {
this.value = value;
this.label = label;
}
public String getValue() {
return this.value;
}
public String getLabel() {
return this.label;
}
@Override
public String toString() {
return label;
}
}
JComboBox test = new JComboBox();
test.addItem(new ComboItem(0, "orange"));
test.addItem(new ComboItem(1, "pear"));
test.addItem(new ComboItem(2, "apple"));
test.addItem(new ComboItem(3, "banana"));
test.setSelectedItem("banana");
Ok, I have modified my question a bit. I forgot that i have a custom item inside my JComboBox that makes it a bit more difficult. i cant do setSelectedItem as i have a ComboItem inside each item. So still, how do i get this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
setSelectedItem("香蕉")
。您可以通过阅读 javadoc。编辑:既然你改变了问题,我就会改变我的答案。
如果您想选择具有“banana”标签的项目,那么您有两种解决方案:
setSelectedIndex(theFoundIndex)
)ComboItem
中的equals
和hashCode
,以便两个具有相同名称的ComboItem
实例是相等的,只需使用setSelectedItem(new ComboItem(anyNumber, "banana"))
;setSelectedItem("banana")
. You could have found it yourself by just reading the javadoc.Edit: since you changed the question, I'll change my answer.
If you want to select the item having the "banana" label, then you have two solutions:
setSelectedItem(theFoundItem)
(orsetSelectedIndex(theFoundIndex)
)equals
andhashCode
inComboItem
so that twoComboItem
instances having the same name are equal, and simply usesetSelectedItem(new ComboItem(anyNumber, "banana"))
;你应该使用模型
You should use model
希望这有帮助:)
Hope this help :)
为什么不采用一个集合(可能是一个 Map(例如 HashMap))并将其用作您自己的实现 ComboBoxModel 接口的组合框模型类的核心?然后,您可以通过键字符串而不是整数轻松访问组合框的项目。
例如...
Why not take a collection, likely a Map such as a HashMap, and use it as the nucleus of your own combo box model class that implements the ComboBoxModel interface? Then you could access your combo box's items easily via their key Strings rather than ints.
For instance...
例如
for example
http://docs .oracle.com/javase/6/docs/api/javax/swing/JComboBox.html#setSelectedItem(java.lang.Object)
有一些警告或潜在的意外行为,如 javadoc 中所述。阅读该内容。
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComboBox.html#setSelectedItem(java.lang.Object)
There are some caveats or potentially unexpected behavior as explained in the javadoc. Make sure to read that.
当组合框由某个类的构造函数填充时设置选定项目的正确方法(如@milosz 发布):
在您的情况下,代码将是:
The right way to set an item selected when the combobox is populated by some class' constructor (as @milosz posted):
In your case the code would be:
只需在执行
comboBox.setSelectedItem
或comboBox.setSelectedIndex
或comboModel.setSelectedItem
后调用comboBox.updateUI()
Just call
comboBox.updateUI()
after doingcomboBox.setSelectedItem
orcomboBox.setSelectedIndex
orcomboModel.setSelectedItem
在我的例子中,构建类 Item(key, value) 作为组合框的项目
In my case build class Item(key, value) as item of combobox