ActionListener and ItemListener not implementing correctly

241 views Asked by At

My task is again to modify my BMI Calculator. My previous code was working correctly but this time, I have added an ItemListener for my radio buttons. I need to store and save the state of the radio buttons currently selected, and then according to their selected state, the Calculate button should perform its calculation in Metric or Imperial. When I run the code, there are no syntax errors. Instead there is a logical error since nothing is being done when I press calculate button after entering weight and height and selecting my radio buttons.

I have created a Boolean variables here to determine what has been selected:

private Boolean selected = false;  //selected is for metric and imperial
private Boolean Gselected = false;  // Gselected is for gender

These are outside, and above my constructor and act as global variable.

I think my problem lies here, when i use them here in a inner private class:

    private class bmiItemListener implements ItemListener
   {


      public void itemStateChanged(ItemEvent event)
      {


    if (metric.isSelected() && male.isSelected() ) {
                selected = true;
        Gselected = true;
         } 
    else if (imperial.isSelected() && male.isSelected()) {

                selected = false;
        Gselected = true;
     }

    if (metric.isSelected() && female.isSelected() ) {
                selected = true;
        Gselected = false;
         } 
    else if (imperial.isSelected() && female.isSelected()) {

                selected = false;
        Gselected = flase;
     }
   }

This is my action listener for my calculate button:

    private class bmiActionListener implements ActionListener
   {

      public void actionPerformed (ActionEvent event)
      {
         String heightText, weightText, bmiTotal;
         double heightVal, weightVal, bmi;


     heightText = height.getText();
     weightText = weight.getText();


     heightVal = Double.parseDouble (heightText) ;
     weightVal = Double.parseDouble (weightText) ;

    if(selected == flase && Gselected == false)
    {
        bmi=(weightVal*703)/(heightVal*heightVal);
        bmiTotal = String.valueOf(bmi);
        resultLabel.setText(bmi ) ;
    }

    else if(selected == true && Gselected == false)
    {
        bmi=weightVal/(heightVal*heightVal)*10000;
        bmiTotal = String.valueOf(bmi);
        resultLabel.setText(bmi ) ;
    }

    else if(selected == false && Gselected == true)
    {
        bmi=(weightVal*703)/(heightVal*heightVal);
        bmiTotal = String.valueOf(bmi);
        resultLabel.setText(bmi ) ;
    }

    else if(selected == false && Gselected == false)
    {
        bmi=(weightVal*703)/(heightVal*heightVal);
        bmiTotal = String.valueOf(bmi);
        resultLabel.setText(bmi ) ;
    }

       }
}
1

There are 1 answers

2
Mike M On

I'm not very familiar with ItemListener, but I would recommend using ActionListener with your radio buttons.

Exmaple:

JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");

male.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        gselected = true;
        female.setSelected(false); //if you can only select male or female
    }
});

female.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        gselected = true;
        male.setSelected(false); //if you can only select male or female
    }
});

EDIT: haven't tested this code or coded for all cases of button presses. just for a general idea