Search code examples
javaswingjframejradiobuttonjspinner

How to select multiple radio buttons and calculate total price?


I am implementing a coffee shop menu and have some troubles with radio buttons and spinners.

How can I calculate the total price for multiple radio buttons each with a subtotal price? Because for now, I can calculate the total for just one product.

Should I add a radio button group or something like that?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    double Total = 0.0;
    if(rbCappucino.isSelected()){
        String Cappucino = chboxCappucino.getValue().toString();
         int boom1 = Integer.parseInt(Cappucino);
        Total = boom1 * 1.0;
    }
    else if(rbAmericano.isSelected()){
        String Americano = chboxAmericano.getValue().toString();
         int boom2 = Integer.parseInt(Americano);
        Total = boom2 * 1.50;
    }
    else if(rbLatte.isSelected()){
        String Latte = chboxLatte.getValue().toString();
         int boom3 = Integer.parseInt(Latte);
        Total = boom3 * 2.50;
    }
    
    totalText.setText(Double.toString(Total));
}                                        

This is my result


Solution

  • The javax.swing.JRadioButton was created to select exactly one option so in your case you should add javax.swing.JCheckBox instead