1

I know that it is possible in an event driven program in Java to find out what object caused an event (e.g. JRadioButton was selected, therefore a certain action will take place). My question is, if you have 2 JRadioButtons in a buttongroup, both with action listeners added to them, and you keep selecting from one to the other, is it possible to find out what JRadioButton was previously selected? In other words, if I selected another JRadioButton, is it possible to write code that determines which JRadioButton was previously selected before selecting the current JRadioButton?

public class Drinks extends JPanel implements ActionListener{
double drinksPrice = 2.10;
double noDrinks = 0;
static  String selectedDrink;
JRadioButton btnPepsi = new JRadioButton("Pepsi"); //add a button to choose different drinks

JRadioButton btnMtDew = new JRadioButton("Mt Dew"); 

JRadioButton btnDietPepsi= new JRadioButton("Diet Pepsi");

JRadioButton btnCoffee = new JRadioButton("Coffee");

JRadioButton btnTea = new JRadioButton("Tea");

JRadioButton btnNone = new JRadioButton("None");
JLabel lblDrinksHeading = new JLabel("Choose a drink (each drink is $2.10):");
ButtonGroup drinksButtonGroup = new ButtonGroup();



private static final long serialVersionUID = 1L;

public Drinks(){

    setLayout(new FlowLayout());    //Using GridLayout  

    btnPepsi.setActionCommand(btnPepsi.getText());    //set the ActionCommand to getText so I can retrieve the name for the receipt

    btnMtDew.setActionCommand(btnMtDew.getText());

    btnDietPepsi.setActionCommand(btnDietPepsi.getText());

    btnCoffee.setActionCommand(btnCoffee.getText());

    btnTea.setActionCommand(btnTea.getText());

    btnNone.setActionCommand(btnNone.getText());

    drinksButtonGroup.add(btnPepsi);
    drinksButtonGroup.add(btnMtDew);
    drinksButtonGroup.add(btnDietPepsi);
    drinksButtonGroup.add(btnCoffee);
    drinksButtonGroup.add(btnTea);
    drinksButtonGroup.add(btnNone);
    btnNone.setSelected(true); //set default to "none"

    btnPepsi.addActionListener(this);
    btnMtDew.addActionListener(this);
    btnDietPepsi.addActionListener(this);
    btnCoffee.addActionListener(this);
    btnTea.addActionListener(this);
    btnNone.addActionListener(this);

    add(lblDrinksHeading);

    add(btnPepsi);

    add(btnDietPepsi);

    add(btnMtDew);

    add(btnCoffee);

    add(btnTea);

    add(btnNone);


    repaint();
    revalidate();

    selectedDrink = drinksButtonGroup.getSelection().getActionCommand();
//add the drink price to totalPrice, it is adding it every time though, even if its none
/*if(drinksButtonGroup.getSelection() == btnNone){
    MenuFrame.totalPrice += 0;
    }
    else{
        MenuFrame.totalPrice += drinksPrice;
        }
*/


//      buttonGroup1.getSelection().getActionCommand()

//String selectedDrink = drinksButtonGroup.getSelection().toString();
//class 

}

@Override
public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();

    if(source == btnNone) {

        MenuFrame.totalPrice += 0;
        TaxAndGratuityFrame.subtotalTextField.setText("$" + MenuFrame.totalPrice);
        TaxAndGratuityFrame.subtotalVariable = MenuFrame.totalPrice;
        TaxAndGratuityFrame.taxVariable = TaxAndGratuityFrame.subtotalVariable * TaxAndGratuityFrame.TAX_RATE;
        TaxAndGratuityFrame.taxTextField.setText("$" + TaxAndGratuityFrame.taxVariable);
        Receipt.receiptTotal.setText("Total: $" + (MenuFrame.totalPrice));
        Receipt.receiptsubtotal.setText("Subtotal: " + (TaxAndGratuityFrame.subtotalVariable));

    }

    else {

        MenuFrame.totalPrice += drinksPrice;
        TaxAndGratuityFrame.subtotalTextField.setText("$" + MenuFrame.totalPrice);
        TaxAndGratuityFrame.subtotalVariable = MenuFrame.totalPrice;
        TaxAndGratuityFrame.taxVariable = TaxAndGratuityFrame.subtotalVariable * TaxAndGratuityFrame.TAX_RATE;
        TaxAndGratuityFrame.taxTextField.setText("$" + TaxAndGratuityFrame.taxVariable);
        Receipt.receiptTotal.setText("Total: $" + (MenuFrame.totalPrice));
        Receipt.receiptsubtotal.setText("Subtotal: " + (TaxAndGratuityFrame.subtotalVariable));

    }

}

Edit: I'll be more specific. I am creating an "imaginary" restaurant program. In it, I list several drinks that have the same price (e.g. Pepsi: $2.10, Mountain Due: $2.10, etc). These listed drinks are JRadioButtons. Once a customer clicks one of these buttons to "order a drink", $2.10 will be added to a "total" variable. However, a problem occurs when a user wants to change there drink, because if they click a different JRadioButton, $2.10 will still be added to the "total" variable. I want to make it so that they can change there drink without adding $2.10 to the order every time.

MoneyC
  • 7
  • 3

2 Answers2

1

I think the problem is at this line:

MenuFrame.totalPrice += drinksPrice;

Because "+=" increments a value by another value instead of simply adding two values.

So every time the user clicks on one of the radio buttons, it will continuously add 2.10 to your total value, whereas if you were you just say:

MenuFrame.totalPrice = MenuFrame.totalPrice + drinksPrice;

It will set your total price equal to the current total price plus the price of drinks, instead of adding 2.10 to the total value every time a button is pressed.

A. Cucci
  • 170
  • 10
0

Perhaps I am misunderstanding the question, but I am thinking along the lines of creating a public variable, and then inside the action listeners updating the variable with the radio button that was just selected. When the selected event fires, you can look at the variable to see which radio button had last been selected, do what you want to about it, and then update it with the new radio button.

MattCash
  • 81
  • 2
  • 9
  • I'll be more specific. I am creating an "imaginary" restaurant program. In it, I list several drinks that have the same price (e.g. Pepsi: $2.10, Mountain Due: $2.10, etc). These listed drinks are JRadioButtons. Once a customer clicks one of these buttons to "order a drink", $2.10 will be added to a "total" variable. However, a problem occurs when a user wants to change there drink, because if they click a different JRadioButton, $2.10 will still be added to the "total" variable. I want to make it so that they can change there drink without adding $2.10 to the order every time. – MoneyC Nov 29 '16 at 01:14
  • I understand now. Without seeing existing code it is not easy to tell you what you should do. You did mention a "total" variable. Does the option exist for the user to change their mind and decide not to order a drink once they have selected one? – MattCash Nov 29 '16 at 01:19
  • I am sorry, I did not mean to send the response above yet... I was going to suggest maybe storing a "drinkTotals" variable, in which if you select a radio button that represents a drink you store the amount to, if they change the drink you change that amount in the variable, and if the select no drink you store 0 to it. Then use it (and other variables) to sum the "total" variable. Do you have your existing code? – MattCash Nov 29 '16 at 01:23
  • Yes. There is another JRadioButton that just says "None". It would just add 0 to the "total" variable. – MoneyC Nov 29 '16 at 01:24
  • I added the code. Hopefully you can understand it. MenuFrame, TaxAndGratuityFrame, and Receipt are other classes – MoneyC Nov 29 '16 at 01:37
  • I believe what is required is a different approach to updating and summing the order's total. For instance, if the user selects a drink, you add 2.10 to the total, but if they then select no drink you add 0 (instead of subtracting 2.10). If all of the order process is the same as drinks (radio button selections), breaking your "total" variable up into many sub-total variables and updating them as they select a radio button is an easy and straight forward approach. – MattCash Nov 29 '16 at 01:39
  • That sounds do-able. Thanks for your help. – MoneyC Nov 29 '16 at 01:49