java - JComboBox Select and Item and Based off of the selection display a new list -


i have following code based off of user selection list displayed:

    private class sheeptypelisthandler implements actionlistener{     @override     public void actionperformed(actionevent e) {         jcombobox<string> cb = (jcombobox<string>)(e.getsource());         if (cb.getselecteditem().equals(sheeptype[0])) {             (string item : eweclass )             sheepclasscb.additem(item);         }         else if (cb.getselecteditem().equals(sheeptype[1])) {             (string item : ramclass )                 sheepclasscb.additem(item);             (string item : weight3 )                 weightcb.additem(item);         }         else if (cb.getselecteditem().equals(sheeptype[2])) {             (string item : lambclass )                 sheepclasscb.additem(item);         }     }     } //end sheeptypelisthandler class 

currently, depending on select new list pop up. however, if change mind , select new item, instead of overriding there adds new list bottom of first list.

for example: if user selects sheeptype[2] following list appears:

finishing early-weaned 

now if change mind , choose sheeptype[0] instead of list:

maintenance nonlactating, first 15 weeks gestation last 6 wks gestation or last 8 wks lactation suckling singles first 8 wks lactation suckling singles or last 8 wks lactation suckling twins first 8 weeks lactation suckling twins replacement lambs , yearlings 

i this:

finishing early-weaned maintenance nonlactating, first 15 weeks gestation last 6 wks gestation or last 8 wks lactation suckling singles first 8 wks lactation suckling singles or last 8 wks lactation suckling twins first 8 weeks lactation suckling twins replacement lambs , yearlings 

how alter current code override when new selection made instead of adding list? thanks!

update: part of code edited following:

private class sheeptypelisthandler implements actionlistener{     @override     public void actionperformed(actionevent e) {         sheepclasscb.removeallitems();          if (sheeplistcb.getselecteditem().equals(sheeptype[0])) {             //sheepclasscb.removeallitems(); //this did not work             (string item : eweclass ){                 sheepclasscb.additem(item);             }         }         else if (sheeplistcb.getselecteditem().equals(sheeptype[1])) {             (string item : ramclass )                 sheepclasscb.additem(item);             (string item : weight3 )                 weightcb.additem(item);         }         else if (sheeplistcb.getselecteditem().equals(sheeptype[2])) {             (string item : lambclass )                 sheepclasscb.additem(item);         }     } } 

now first time choose item out of list works. if reselect different item shows empty list.

it seems missing line sheepclasscb.removeallitems(). made simple example based on code:

public class listempty extends jframe {      jcombobox<string> changingcb = new jcombobox<>();     jcombobox<string> unchangingcb = new jcombobox<>();      string[] numbers = new string[] {"1", "2", "3"};     string[] letters = new string[] {"a", "b", "c"};     string[] symbols = new string[] {"!", "@", "#"};      listempty() {          unchangingcb.additem("numbers");         unchangingcb.additem("letters");         unchangingcb.additem("symbols");         unchangingcb.addactionlistener(new sheeptypelisthandler());          getcontentpane().add(unchangingcb, borderlayout.line_start);         getcontentpane().add(changingcb, borderlayout.center);          setlocationrelativeto(null);         setdefaultcloseoperation(exit_on_close);         pack();         setvisible(true);     }      private class sheeptypelisthandler implements actionlistener {          @override         public void actionperformed(actionevent e) {              changingcb.removeallitems(); // compare removing line.             if (unchangingcb.getselecteditem().equals("numbers")) {                 (string item : numbers)                     changingcb.additem(item);             } else if (unchangingcb.getselecteditem().equals("letters")) {                 (string item : letters)                     changingcb.additem(item);             } else if (unchangingcb.getselecteditem().equals("symbols")) {                 (string item : symbols)                     changingcb.additem(item);             }         }     }      public static void main(string[] args) {          new listempty();     } } 

you may want consider switch statement instead of multiple if-else statements.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -