I assume you are referring to getSelectedItem() ?
The reason is that if the combo box is editable, the selected item is not necessarily contained in the backup model and is not limited to the general type. For example. if you have an editable JComboBox<Integer> with the model [1, 2, 3], you can still enter "foo" in the component, and getSelectedItem() will return the string "foo", not an object of type Integer.
If the combo box is not editable, you can always cb.getItemAt(cb.getSelectedIndex()) to cb.getItemAt(cb.getSelectedIndex()) for type safety. If nothing is selected, this will return null , which corresponds to the behavior of getSelectedItem() .
jarnbjo Aug 11 2018-11-11T00: 00Z
source share