java - Why does ComboBoxModel<E> return Object instead of E -
this question has answer here:
using swing's comboboxmodel
, type casting required when getting selected element, interface defined follows:
public interface comboboxmodel<e> extends listmodel<e> { void setselecteditem(object anitem); object getselecteditem(); }
i think return type of getselecteditem
e
. in fact, done listmodel
interface comboboxmodel
inherits from, selecting index:
public interface listmodel<e> { e getelementat(int index); }
what reason not use e
type parameter in comboboxmodel
?
because combobox textfield may editable user.
jcombobox.seteditable(true);
if combobox editable user may enter text combobox textfield returned string jcombobox.getselecteditem() no matter type parameter provided model.
if want object of e use:
e e = jcombobox.getmodel().getelementat(jcombobox.getselectedindex());
Comments
Post a Comment