How to set selected index JComboBox by value
You should use model
comboBox.getModel().setSelectedItem(object);
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:
- Iterate through the items to find the one (or the index of the one) which has the given label, and then call
setSelectedItem(theFoundItem)
(orsetSelectedIndex(theFoundIndex)
) - Override
equals
andhashCode
inComboItem
so that twoComboItem
instances having the same name are equal, and simply usesetSelectedItem(new ComboItem(anyNumber, "banana"))
;