Brandon
Brandon

Reputation: 161

Action Listener

The below code is an action listener for a project. Basically, I have 4 radio buttons and when I click one, I want it to change a variable on screen. When I run the code, it is simply adding together all the values. Are there any other ways to do this?

class Calc implements ActionListener
    {
     public void actionPerformed(ActionEvent event)
     {
       double base = 0.00;
       double options;
       double total;

      if (Button25.isSelected());
      {
      base = base + 999.99;
      String base2 = Double.toString(base);
      lblBaseAns.setText(base2);
      }

      if (Button32.isSelected());
      {
        base = base + 1049.99;
        String base2 = Double.toString(base);
        lblBaseAns.setText(base2);
      }

      if (Button35.isSelected());
      {
        base = base + 1099.99;
        String base2 = Double.toString(base);
        lblBaseAns.setText(base2);
      }

      if (Button42.isSelected());
      {
        base = base + 1155.99;
        String base2 = Double.toString(base);
        lblBaseAns.setText(base2);
      }


     }
    }

Upvotes: 1

Views: 424

Answers (1)

wattostudios
wattostudios

Reputation: 8764

The problem is that, for each if() statement, such as if (Button32.isSelected());, you have a ; symbol at the end. This shouldn't be there. Here is the corrected code...

class Calc implements ActionListener {
    public void actionPerformed(ActionEvent event){
        double base = 0.00;
        double options;
        double total;

        if (Button25.isSelected()){ // changed
            base = base + 999.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
        else if (Button32.isSelected()){ // changed
            base = base + 1049.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
        else if (Button35.isSelected()){ // changed
            base = base + 1099.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
        else if (Button42.isSelected()){ // changed
            base = base + 1155.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
    }
}

As an alternative, why don't you get the button that was clicked from the ActionEvent, and then use the button in your if-else branch...

class Calc implements ActionListener {
    public void actionPerformed(ActionEvent event){
        double base = 0.00;
        double options;
        double total;

        Object clickedObject = event.getSource();
        if (clickedObject instanceof JRadioButton){
            JRadioButton clickedButton = (JRadioButton)clickedObject;

            if (clickedButton == Button25){
                base = base + 999.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
            else if (clickedButton == Button32){
                base = base + 1049.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
            else if (clickedButton == Button35){
                base = base + 1099.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
            else if (clickedButton == Button42){
                base = base + 1155.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
        }
    }
}

Upvotes: 2

Related Questions