java - JComboBox, ActionListener, How do I really use them? -


im learning java , stuck onto jcombobox. have feel things trying out , hitting wall pass 4 hours.

i trying let user select 1-10 combobox. how value of combobox? value of combo box equivalent quantity.

so have value maybe $10. if user choose quantity 2.

i want value of user choose, take value of $10 , times 2.

the result $20 displayed on jtextfield.

please :(

public class panel extends jpanel {      public panel(){         jpanel test = new jpanel(new gridbaglayout());          string[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};         jcombobox quantitiescb = new jcombobox(quantities1);         quantitiescb.addactionlistener(                 new actionlistener(){                     public void actionperformed(actionevent e){                         jcombobox combo = (jcombobox)e.getsource();                         string currentquantity = (string)combo.getselecteditem();                     }                 }                     );          jtextfield result = new jtextfield();          setlayout(new gridbaglayout());         setpreferredsize(new dimension(640,480));         gridbagconstraints gbc = new gridbagconstraints();          gbc.gridx = 0;         gbc.gridy = 0;         gbc.weightx = 0.1;         gbc.weighty = 0.1;         gbc.fill = gridbagconstraints.horizontal;         gbc.anchor = gridbagconstraints.north;         add(quantitiescb, gbc);     }  } 

just few changes:

public class panel extends jpanel {      public panel(){         jpanel test = new jpanel(new gridbaglayout());         string value = "10";         final jtextfield result = new jtextfield();          string[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};         jcombobox quantitiescb = new jcombobox(quantities1);         quantitiescb.addactionlistener(                 new actionlistener(){                     public void actionperformed(actionevent e){                         jcombobox combo = (jcombobox)e.getsource();                         string currentquantity = (string)combo.getselecteditem();                         int value1 = integer.valueof(value);                         int value2 = integer.valueof(currentquantity);                          string resulttext = string.valueof(value1*value2);                         result.settext("$" + resulttext);                     }                 }                     );          setlayout(new gridbaglayout());         setpreferredsize(new dimension(640,480));         gridbagconstraints gbc = new gridbagconstraints();          gbc.gridx = 0;         gbc.gridy = 0;         gbc.weightx = 0.1;         gbc.weighty = 0.1;         gbc.fill = gridbagconstraints.horizontal;         gbc.anchor = gridbagconstraints.north;         add(quantitiescb, gbc);     }  } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -