获取 JComboBox 的选定对象位置
我正在尝试获取 JComboBox 对象的位置(作为 int),生成了 ComboBox 并具有像这样的操作侦听器
for (int d=0; d<i; d++)
{
titulos.addItem(listaPraBox[d]);
}
ActionListener comboListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
ItemSelectable is =(ItemSelectable)actionEvent.getSource();
objectoseleccionado = selectedString(is);
DeskMetodos.listaTexto(objectoseleccionado);
}
};
titulos.addActionListener(comboListener);
执行
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
但我希望所选对象的位置通过该 int 从另一个数组中获取字符串。
这可能吗?根据我所做的搜索,甚至没有提到这一点。
I'm trying to get the position (as a int) of a JComboBox object, The ComboBox is generated and haves an action listener like this
for (int d=0; d<i; d++)
{
titulos.addItem(listaPraBox[d]);
}
ActionListener comboListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
ItemSelectable is =(ItemSelectable)actionEvent.getSource();
objectoseleccionado = selectedString(is);
DeskMetodos.listaTexto(objectoseleccionado);
}
};
titulos.addActionListener(comboListener);
The executes
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
But I wanted the position of the selected object to get a string from another array by that int.
Is this even possible? By the search I've made there isn't even reference to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JComboBox
定义getSelectedIndex()
。实现只是使用getSelectedItem()
循环数据模型检查相等性。这并不构成
ItemSelectable
,但数据模型本身也不构成,因此您可能需要使用具体类。JComboBox
definesgetSelectedIndex()
. The implementation is just to loop over the data model checking equality withgetSelectedItem()
.That doesn't make it up into
ItemSelectable
, but neither does the data model itself, so you may need to use the concrete class.而不是将项目存储在 ComboBox 中并且必须使用索引来引用另一个值数组。只需在 ComboBox 中存储一个对象,该对象具有与当前显示值匹配的
toString()
输出以及对数组中对象的直接引用。这样,任何拉动所选项目或处理组合框的对象都可以拉动它们所需的值,而不必“了解”其他数组。Instead of storing items in a ComboBox and having to use the index to reference another array of values. Just store an object in the ComboBox that has a
toString()
output that matches your current displayed value and a direct reference to the object in the array. That way any object pulling the selected item or dealing with comobo box can just pull the value they need and not have to also "know" about this other array.