java - Populating a JComboBox with a selection from another JComboBox -
i’ve got 2 jcomboboxes
, 1 displays objects
of type category
(each category
contains unique list
) , display list
in category
.
the aim allow user select category
in 1st jcombobox
, populate 2nd jcombobox
list
appropriate user selection.
my current code, before user selection, displays 1st jcombobox
objects
of category
type. 2nd displays list
in object
. that’s fine. upon user selection, 2nd jcombobox
doesn’t anything. continues display same list
prior user selection.
here code..
list<category> catlist = control.getcatlist(); edititemcatcombobox.removeallitems(); for(category cat: catlist) //this populates 1st jcombobox { edititemcatcombobox.additem(cat); } string selectedcat = edititemcatcombobox.getselecteditem().tostring(); //maybe line wrong //edititemdialog.validate(); (category cat: catlist) //this block should populate 2nd jcombobox { if(selectedcat.equals(cat.getcatname())) { list<item> itemlist = cat.getcatitems(); edititemitemscombobox.removeallitems(); for(item itm: itemlist) { edititemitemscombobox.additem(itm); } } } edititemdialog.setvisible(true);
i think should implement itemlistener on first jcombobox edititemcatcombobox
contains type category
can update second 1 every time select new category
edititemcatcombobox.additemlistener(new itemlistener() { public void itemstatechanged(itemevent arg0) { //update second jcombobox } });
Comments
Post a Comment