开发者

Getting the selected object position of a JComboBox

开发者 https://www.devze.com 2023-03-19 08:08 出处:网络
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

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 priva开发者_开发知识库te 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消