开发者

Help in automatically changing value upon ItemChange in JComboBox

开发者 https://www.devze.com 2023-03-26 05:09 出处:网络
I have a program in which I am using 3 things, a checkbox, a combobox and a textfield. The logic works like this if checkbox is enable then combobox and textfield are enable unless not.

I have a program in which I am using 3 things, a checkbox, a combobox and a textfield. The logic works like this if checkbox is enable then combobox and textfield are enable unless not.

Then set some value in the textfield by mulitplying it with the item in combobox.

Help in automatically changing value upon ItemChange in JComboBox

From the frame - The value of Final Price is Price * Quantity.

Now the issue when I click purchase everything went fine. But when I change the value from Jcombobox it doesn't automatically change the value in final price and remains to be 1200 as in first case. For the value to be changed I have uncheck and then check the Checkbox.

What could be the problem. I have used ItemListner for both checkbox and combobox.

@Override
public void itemStateChanged(ItemEvent e){

    Object get = e.getSource();

    int multiplier;
    int ftotal;


    if (e.getStateChange()==ItemEvent.SELECTED){
        if(get==ch开发者_如何学编程kbox1){
             qntbox1.setEnabled(true);            
             size1.setEnabled(true);
             multiplier = Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()));


             ftotal = Integer.parseInt(price1.getText()) * multiplier;
             fprice1.setText(String.valueOf(ftotal));}


You have to implement ActionListener for your JComboBox:

private static final String command_cbo1 = "ComboBox1";
// ...

public class YourClass implements ItemListener, ActionListener
{
    // ...

    public YourClass()
    {
        // ...
        qntbox1.addActionListener(this);
        qntbox1.setActionCommand(command_cbo1);
        // ...
    }

    // ...

    public void itemStateChanged(ItemEvent e)        
    {
        // ...
    }

    // ...

    public void actionPerformed(ActionEvent e)
    {
        JComboBox cb = (JComboBox) e.getSource();
        String s = (String) cb.getSelectedItem();

        if(e.getActionCommand().equals(command_cbo1))
        {
            fprice1.setText("" + (Integer.parseInt(price1.getText()) * Integer.parseInt(s)));
        }
        // ...
    }

    // ...
}


not directly to your question

1/ JCheckBox is totally useless, that will be really needed for final calculation(s)

2/ consider that JComponents for Price and Final Price would be only JFormattedTextField, then you can pretty to forgot for Parse#Whatever

3/ consider that JComponents for Quantity would be only JSpinner, but workaround for Number Instance would be litte bit complicated as for JFormattedTextField example here

4/ for nice output put everything to the JTable

5/ for JComboBox I preferred ItemListener not ActionListener, because your problems isn't with proper Listener but with parsing Numbers correct way


Ok got it working. The ActionListner made it work (JComboBox). I guess using ItemListner for too many components made parsing a little confusing, add to that I used too many clauses in the ItemListner scope. Thanks a lot everyone for helping.

@mKorbel : I'll be using your suggestion asap :) and will check JTable and said components. have to go through them since I haven't used it.

@Eng.Fouad : Thanks man for the help.

Just one issue. When I typecast getSelectedItem() to integer it gives NumberFormatException error (runtime). So I have to first change the object to String and then parseit into integer. Any clue why direct conversion is throwing error ?

Here is the working code for the project.

public void itemStateChanged(ItemEvent e){

    Object get = e.getSource();



    if (e.getStateChange()==ItemEvent.SELECTED){
        if(get==chkbox1){
             qntbox1.setEnabled(true);            
             size1.setEnabled(true);   
             fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()))));
        }
  @Override
       public void actionPerformed (ActionEvent ae)
       {

          Object toggel = ae.getSource();      
          String check;

          if (toggel == qntbox1)
          {
            check = (String) qntbox1.getSelectedItem();
            fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(check)));

          }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号