java - Unable to make my itemListener select the appropriate action based on its source -
i making application school supposed accept locations 2 choice boxes , set mile distance between locations textfield. problem textfields not getting numbers intended. in checking teacher found itemstatechange method not reading sources of itemlisteners. can tell me wrong seem unable teach myself particular concept. relevant code issue follows:
//connect actionlisteners , itemlisteners submit.addactionlistener(this); reset.addactionlistener(this); fromchoice.additemlistener(this); tochoice.additemlistener(this); vehicletype.additemlistener(this); gastype.additemlistener(this); submit.setactioncommand("submit"); reset.setactioncommand("reset");
...
public void actionperformed(actionevent e) { string arg = e.getactioncommand(); if (arg == "submit") { string val1 = miles.gettext(); string val2 = gasprice.gettext(); double double1 = double.parsedouble(val1); double double2 = double.parsedouble(val2); total = double1 * double2; totalpriceresult.settext(total.tostring()); } else if (arg == "reset") { gasprice.settext(""); miles.settext(""); } } public void itemstatechanged(itemevent c) { object source = c.getitemselectable(); if (source == gastype) { if (c.getstatechange() == itemevent.selected) { int gaschoice = gastype.getselectedindex(); if (gaschoice == 0) { gasprice.settext ("3.00"); } else if (gaschoice == 1) { gasprice.settext("2.90"); } else if (gaschoice == 2) { gasprice.settext("2.50"); } else if (gaschoice == 3) { gasprice.settext("4.00"); } } } else if (source == fromchoice) { getmiles(); } else if(source == tochoice) { getmiles(); } }
Comments
Post a Comment