获取 JComboBox 的选定对象位置

发布于 2024-11-19 14:04:49 字数 861 浏览 2 评论 0原文

我正在尝试获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

止于盛夏 2024-11-26 14:04:49

JComboBox 定义 getSelectedIndex()。实现只是使用 getSelectedItem() 循环数据模型检查相等性。

这并不构成 ItemSelectable,但数据模型本身也不构成,因此您可能需要使用具体类。

JComboBox defines getSelectedIndex(). The implementation is just to loop over the data model checking equality with getSelectedItem().

That doesn't make it up into ItemSelectable, but neither does the data model itself, so you may need to use the concrete class.

苏别ゝ 2024-11-26 14:04:49

而不是将项目存储在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文