java - JFrame GUI addition method not working -
okay have gui takes text input (say 10) , can check box want add initial amount (four choices 10, 20, 50, 100). after trying display sum of items in text frame.
int firstmuffler = integer.parseint(inputmufflers.gettext()); int firsttire = integer.parseint(inputtires.gettext()); int firstbreaks = integer.parseint(inputbreaks.gettext()); int firstmirrors = integer.parseint(inputmirrors.gettext()); if (muf10.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 10)); } else if (muf20.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 20)); } else if (muf50.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 50)); } else if (muf100.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 100)); } else if (t10.isselected()){ finalstockt.settext(string.valueof(firsttire + 10)); } else if (t20.isselected()){ finalstockt.settext(string.valueof(firsttire + 20)); } else if (t50.isselected()){ finalstockt.settext(string.valueof(firsttire + 50)); } else if (t100.isselected()){ finalstockt.settext(string.valueof(firsttire + 100)); } else if (b10.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 10)); } else if (b20.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 20)); } else if (b50.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 50)); } else if (b100.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 100)); } else if (m10.isselected()){ finalstockm.settext(string.valueof(firstmirrors + 10)); } else if (m20.isselected()){ finalstockm.settext(string.valueof(firstmirrors+ 20)); } else if (m50.isselected()){ finalstockm.settext(string.valueof(firstmirrors + 50)); } else if (m100.isselected()){ finalstockm.settext(string.valueof(firstmirrors + 100)); }else{ }
this have, can't figure out making not work. because outputs finalstockmuf
text box , rest don't work. have fill out make work, zeros.
note: muf10
, t10
, etc. check boxes values. firsttire
, firstmug
, etc. first price input, without check boxes checked.
when use else if
ladder, need remember 1 option, meets requirements, executed. in example, implemented code responsible collecting , inserting data from/to multiple objects in 1 else if ladder. in effect, first option satisfied condition worked, , rest ignored. therefore, should change code this:
if (muf10.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 10)); } else if (muf20.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 20)); } else if (muf50.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 50)); } else if (muf100.isselected()){ finalstockmuf.settext(string.valueof(firstmuffler + 100)); } //break , beginning of new else if structure, if (t10.isselected()){ finalstockt.settext(string.valueof(firsttire + 10)); } else if (t20.isselected()){ finalstockt.settext(string.valueof(firsttire + 20)); } else if (t50.isselected()){ finalstockt.settext(string.valueof(firsttire + 50)); } else if (t100.isselected()){ finalstockt.settext(string.valueof(firsttire + 100)); } //break , beginning of new else if structure, if (b10.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 10)); } else if (b20.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 20)); } else if (b50.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 50)); } else if (b100.isselected()){ finalstockb.settext(string.valueof(firstbreaks + 100)); } //break , beginning of new else if structure, if (m10.isselected()){ finalstockm.settext(string.valueof(firstmirrors + 10)); } else if (m20.isselected()){ finalstockm.settext(string.valueof(firstmirrors+ 20)); } else if (m50.isselected()){ finalstockm.settext(string.valueof(firstmirrors + 50)); } else if (m100.isselected()){ finalstockm.settext(string.valueof(firstmirrors + 100)); }
and should work fine. next problem numberformatexception: input string: ""
, thrown when input text field empty. solve use:
int firstmuffler = (!inputmufflers.gettext().equals("")) ? integer.parseint(inputmufflers.gettext()) : 0;
...and similar solution every next jtextfield
. code checks if jtextfield
empty, , if is, gives 0 value firstmuffler
. doesn't fill jtextfield
'0'.
to fill output jtextfield
not modified value of stock or input , output jtextfield '0' if there no input, enough add example:
}else if(inputmufflers.gettext().equals("")){ inputmufflers.settext("0"); finalstockmuf.settext("0"); }else{ finalstockmuf.settext(inputmufflers.gettext()); }
to end of every else if
part. hope find answer useful.
Comments
Post a Comment