Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have an assignment where I am trying to sum the value from two button groups i

ID: 664119 • Letter: I

Question

I have an assignment where I am trying to sum the value from two button groups in Java after an action happens. I've got two button groups but they both call the same action listener. I would expect the subject button & location button values to sum. For example, inStudio (0.00) + aPet (95.00) = 95.00

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DortJRadioButtonDemo extends JFrame
{
/* private final double singlePerson = 40.00,
twoOrMorePPL = 75.00,
aPet = 95.00,

inStudio = 0.00,
outStudio = 90.00;*/

RadioListener myListener = null;
JLabel cost = new JLabel("Cost: ");

  
public DortJRadioButtonDemo()
{
super("~*~*~*~ PAULA's PORTRAITS ~*~*~*~");
setLayout(new FlowLayout());
setSize(800, 800);
JLabel introLabel = new JLabel("Welcome. Left, please pick the subjects. Right, please pick the location. Then we will tell you the cost!");


//subject radio buttons in their own group   
JRadioButton singlePersonRadio = new JRadioButton("Single Person");
JRadioButton twoOrMorePPLRadio = new JRadioButton("Two + People");
JRadioButton aPetRadio = new JRadioButton("A pet");
  
ButtonGroup sGroup = new ButtonGroup();
       sGroup.add(singlePersonRadio);
       sGroup.add(twoOrMorePPLRadio);
       sGroup.add(aPetRadio);
  
//location radio buttons in their own group
JRadioButton inStudio = new JRadioButton("In Studio");
JRadioButton outStudio = new JRadioButton("Out of Studio");
  
ButtonGroup lGroup = new ButtonGroup();
lGroup.add(inStudio);
lGroup.add(outStudio);
  
//add stuff to your frame
add(introLabel);
add(singlePersonRadio);
add(twoOrMorePPLRadio);
add(aPetRadio);
add(inStudio);
add(outStudio);

add(cost);

//action listener to each radio button
myListener = new RadioListener();

singlePersonRadio.setActionCommand("40.00");
twoOrMorePPLRadio.setActionCommand("75.00");
aPetRadio.setActionCommand("95.00");
  
inStudio.setActionCommand("0.00");
outStudio.setActionCommand("90.00");
  
singlePersonRadio.addActionListener(myListener);
       twoOrMorePPLRadio.addActionListener(myListener);
       aPetRadio.addActionListener(myListener);
  
inStudio.addActionListener(myListener);
       outStudio.addActionListener(myListener);

       setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

class RadioListener implements ActionListener
{

       public void actionPerformed(ActionEvent e)
{
System.out.println("Something" + e.getActionCommand());
cost.setText("Cost: " + e.getActionCommand());


       }
   }

public static void main(String[] args)
{
    // Run the program now.
DortJRadioButtonDemo test = new DortJRadioButtonDemo();
   test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


}

Explanation / Answer

Note:

There are two ways to perform the action. The code added to the existing code is highlighted in bold letters.

First Way:

Create another ActionListener class called RadioListener1 and create an object to the class as myListener1.

Add the second group’s radio buttons to this listener.

When displaying the cost , first get the first group value and store it in a variable.

In the second group while displaying the cost, add the first group value to the second group value and display it.

Add the below code at the respective locations to the existing code as follows:

Inside the class DortJRadioButtonDemo:

RadioListener1 myListener1 = null;

static double x, y;

ButtonGroup sGroup, lGroup;

Inside the DortJRadioButtonDemo constructor:

Inside the DortJRadioButtonDemo class, create another ActionListener class with the following code:

class RadioListener1 implements ActionListener

     {

          public void actionPerformed(ActionEvent e)

          {

              //add the first group value to the second group

              //and display the value

              System.out.println("Something" + e.getActionCommand());

              y= Double.parseDouble(lGroup.getSelection().getActionCommand());          

              System.out.println(y);

              cost.setText("Cost: " +(x+y));

              System.out.println("Cost: "+(x+y));

          }

     }

Here is the program code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DortJRadioButtonDemo extends JFrame
{
     //create RadioListener objects
     RadioListener myListener = null;
     RadioListener1 myListener1 = null;

     //declare two static variables
     static double x, y;
     JLabel cost = new JLabel("Cost: ");

     //ButtonGroup variables
     ButtonGroup sGroup, lGroup   

    JPanel jpan=new JPanel(new GridLayout(4, 1, 3, 5));
     JPanel jpan1 = new JPanel(new GridLayout(3, 2));

     public DortJRadioButtonDemo()
     {
          super("~*~*~*~ PAULA's PORTRAITS ~*~*~*~");
          setLayout(new FlowLayout());
          setSize(800, 800);
          JLabel introLabel = new JLabel(
                   "Welcome. Left, please pick the subjects. Right, please pick the location. Then we will tell you the cost!");

          // subject radio buttons in their own group
          JRadioButton singlePersonRadio = new JRadioButton("Single Person");
          JRadioButton twoOrMorePPLRadio = new JRadioButton("Two + People");
          JRadioButton aPetRadio = new JRadioButton("A pet");

          sGroup = new ButtonGroup();        
          sGroup.add(singlePersonRadio);
          sGroup.add(twoOrMorePPLRadio);
          sGroup.add(aPetRadio);

          // location radio buttons in their own group
          JRadioButton inStudio = new JRadioButton("In Studio");
          JRadioButton outStudio = new JRadioButton("Out of Studio");

          lGroup = new ButtonGroup();
          lGroup.add(inStudio);
          lGroup.add(outStudio);

          jpan.add(introLabel);
          jpan1.add(singlePersonRadio);
          jpan1.add(inStudio);
          jpan1.add(twoOrMorePPLRadio);
          jpan1.add(outStudio);
          jpan1.add(aPetRadio);
          jpan1.add(new JLabel());
          jpan.add(jpan1);
          jpan.add(cost);
          add(jpan);

          // two action listener to each radio button's ButtonGroup
          myListener = new RadioListener();
          myListener1 = new RadioListener1();

          singlePersonRadio.setActionCommand("40.00");
          twoOrMorePPLRadio.setActionCommand("75.00");
          aPetRadio.setActionCommand("95.00");

          inStudio.setActionCommand("0.00");
          outStudio.setActionCommand("90.00");        

          //add one listener to one button group
          singlePersonRadio.addActionListener(myListener);
          twoOrMorePPLRadio.addActionListener(myListener);
          aPetRadio.addActionListener(myListener);

          //add another listener to another button group
          inStudio.addActionListener(myListener1);
          outStudio.addActionListener(myListener1);

          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     //implement the frist ActionListener interface
     class RadioListener implements ActionListener
     {
          public void actionPerformed(ActionEvent e)
          {
              //get the first group value
              System.out.println("Something" + e.getActionCommand());
              x = Double.parseDouble(sGroup.getSelection().getActionCommand());        

              System.out.println(x);
              cost.setText("Cost: " + x);
          }
     }

     //implement the second ActionListener interface
     class RadioListener1 implements ActionListener
     {
          public void actionPerformed(ActionEvent e)
          {
              //add the first group value to the second group
              //and display the value
              System.out.println("Something" + e.getActionCommand());            
              double y= Double.parseDouble(lGroup.getSelection().getActionCommand());         

              System.out.println(y);
              cost.setText("Cost: " +(x+y));
              System.out.println("Cost: "+(x+y));
          }
     }

     public static void main(String[] args)
     {
          // Run the program now.
          DortJRadioButtonDemo test = new DortJRadioButtonDemo();
          test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
}

Sample Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote