0

I'm new to Java, and I am having trouble returning values (prices) represented by radio buttons and checkboxes. I've never done this before, so I'm not actually sure where to even begin. Here is what I have:

public class Size extends JPanel {

JLabel lblSize;
JRadioButton radioSmall;
JRadioButton radioMedium;
JRadioButton radioLarge;
double cost = 0.00;
String size = "Unknown Size";

public Size() {
    createPanel();
}

private void createPanel() {
    super.setLayout(new GridBagLayout());
    GridBagConstraints bag = new GridBagConstraints();
    ButtonGroup group = new ButtonGroup();

    bag.fill = GridBagConstraints.BOTH;
    bag.anchor = GridBagConstraints.FIRST_LINE_START;
    bag.insets = new Insets(5,5,5,5);

    bag.gridx = 0;
    bag.gridy = 0;
    bag.gridwidth = 2;
    lblSize = new JLabel("What size pizza?");
    lblSize.setFont(new Font("Arial", Font.BOLD, 12));
    lblSize.setForeground(Color.BLUE);
    this.add(lblSize, bag);

    bag.gridx = 0;
    bag.gridy = 1;
    radioSmall = new JRadioButton("Small");
    radioSmall.setActionCommand("8.00");
    group.add(radioSmall);
    this.add(radioSmall, bag);

    bag.gridx = 0;
    bag.gridy = 2;
    radioMedium = new JRadioButton("Medium");
    radioMedium.setActionCommand("10.00");
    group.add(radioMedium);
    this.add(radioMedium, bag);

    bag.gridx = 0;
    bag.gridy = 3;
    radioLarge = new JRadioButton("Large");
    radioLarge.setActionCommand("12.00");
    group.add(radioLarge);
    this.add(radioLarge, bag);
}

ActionListener radioButtonActionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("8.00")) {
            cost = 8.00;
            size = "Small";
        }
        else if (e.getActionCommand().equals("10.00")) {
            cost = 10.00;
            size = "Medium";
        }
        else if (e.getActionCommand().equals("12.00")) {
            cost = 12.00;
            size = "Large";
        }
        else {
            cost = 0.00;
        }
    }
};

public double getCost() {
    return cost;
}

public String getPizzaSize() {
    return size;
}

}

And here is where I'm trying to implement checkboxes. Can someone please point me in the right direction?:

public class Toppings extends JPanel {

JLabel lblToppings;
JCheckBox boxPepperoni;
JCheckBox boxSausage;
JCheckBox boxMushroom;
JCheckBox boxOnion;

double toppingCost = 0.00;
StringBuilder str = new StringBuilder();

public Toppings() {
    createPanel();
}

private void createPanel() {
    super.setLayout(new GridBagLayout());
    GridBagConstraints bag = new GridBagConstraints();

    bag.fill = GridBagConstraints.BOTH;
    bag.anchor = GridBagConstraints.FIRST_LINE_START;
    bag.insets = new Insets(5,5,5,5);

    bag.gridx = 0;
    bag.gridy = 0;
    boxPepperoni = new JCheckBox("Pepperoni");
    boxPepperoni.addItemListener(new CheckBoxListener());
    this.add(boxPepperoni, bag);

    bag.gridx = 1;
    bag.gridy = 0;
    boxSausage = new JCheckBox("Sausage");
    boxSausage.addItemListener(new CheckBoxListener());
    this.add(boxSausage, bag);

    bag.gridx = 0;
    bag.gridy = 1;
    boxMushroom = new JCheckBox("Mushroom");
    boxMushroom.addItemListener(new CheckBoxListener());
    this.add(boxMushroom, bag);

    bag.gridx = 1;
    bag.gridy = 1;
    boxOnion = new JCheckBox("Onion");
    boxOnion.addItemListener(new CheckBoxListener());
    this.add(boxOnion, bag);
}

public double getToppingCost() {
    return toppingCost;
}

public String getToppings() {
    return str.toString();
}

private class CheckBoxListener implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource()==boxPepperoni) {
            if(boxPepperoni.isSelected()) {
                toppingCost += 2.00;
                str.append("Pepperoni");
            }
        }
        else if (e.getSource()==boxSausage) {
            if(boxSausage.isSelected()) {
                toppingCost += 2.00;
                str.append("Sausage");
            }
        }
        else if (e.getSource()==boxMushroom) {
            if(boxMushroom.isSelected()) {
                toppingCost += 2.00;
                str.append("Mushroom");
            }
        }
        else if (e.getSource()==boxOnion) {
            if(boxOnion.isSelected()) {
                toppingCost += 2.00;
                str.append("Onion");
            }
        }
    }
}

} Any help is appreciated.

stevanc
  • 15
  • 6
  • Define what you mean by 'returning values (prices) represented by radio buttons and checkboxes'. Return to what? What behavior do you actually seek? I'd also recommend stripping out unnecessary code - one JRadioButton and/or JCheckBox is sufficient to demonstrate your problem. – copeg Apr 11 '15 at 20:35
  • The program represents a pizza order. So I use the radio buttons to determine if it is small, medium, or large, with the respective prices being registered by the selection. The checkboxes represent toppings on the pizza. There can be any number of these selected and each topping increments the price by $2.00. I have the GUI looking fine, but for some reason it is not returning anything from the selections I make in the GUI. – stevanc Apr 12 '15 at 10:33

1 Answers1

0

You miss the following in your code in createPanel() (I mean the first example)

radioSmall.addActionListener(radioButtonActionListener);
radioMedium.addActionListener(radioButtonActionListener);
radioLarge.addActionListener(radioButtonActionListener);

When you add these calls, your getCost() and getPizzaSize() methods will return cost and size.

Eugene
  • 1,865
  • 3
  • 21
  • 24