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
Post a Comment