I’m new here, and this is my very first post.
I have a very specific need to code this very trivial application only invoking the isSelected() method to check whether or not a JRadioButton or JCheckBox have been selected. This means I do not want to loop through a ButtonGroup and invoke its getSelection() method.
My code is also identical to the one from a textbook authored by Tony Gaddis, which I am currently learning from: Starting Out With Java, From Control Structures through Objects. 4th Edition.
This is a logical problem, as the application compiles and runs without error.
Here’s what’s going on:
I have four classes: BagelsPanel, CoffeePanel, ToppingsPanel and the GUI class, BagelApp – all of which extend JPanel except the BagelApp class which extends a JFrame.
The purpose of the app is to let a user make coffee, bagel and toppings selections and return the total price of all their selections. The problem is it keeps returning $0.00.
My suspicion is, for some reason, isSelected() isn’t recognizing something is selected.
I will post the BagelsPanel and the GUI class code that involves these problems below:
public double calcBagel() {
double total = 0.00;
if(button1.isSelected())
total = PLAIN;
else if(button2.isSelected())
total = WHOLE_WHEAT;
else if(button3.isSelected())
total = CINNA_RAISON;
else if(button4.isSelected())
total = EVERYTHING;
return total;
}
The above is the calcBagel() method in the BagelsPanel class that’s invoking the isSelected() method to check which JRadioButton is selected, then assigning its price to total. Below is the GUI class:
public void buildPanel() {
panel = new JPanel();
calc = new JButton("Calculate");
calc.addActionListener(new ButtonListener());
panel.add(calc);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
double subtotal = 0.0;
double total = 0.0;
bagels = new BagelPanel();
coffee = new CoffeePanel();
toppings = new ToppingsPanel();
subtotal = bagels.calcBagel() + coffee.calcCoffee() +
toppings.calcToppings();
double tax = subtotal * TAX;
total = subtotal + tax;
DecimalFormat dollar = new DecimalFormat("$0.00");
JOptionPane.showMessageDialog(null, "Your total is: "
+ dollar.format(total));
}
}
Here’s some insight: if I change the double variable total, in the calcBagel() method inside the BagelsPanel class to 1.0, and then run the application and click on the JRadioButton “Calculate,” it accurately props up a JOptionPane telling me my total is $1.06 (the final double variable TAX is set at 0.06).
I’d really appreciate any help I can get. I’m still a beginner in Java and don’t quite understand why my logic is incorrect. I’m embarrassed this might be extremely trivial but I’ve checked the book and the code is identical. Any suggestions?
Your problem is right here, in your
actionPerformedmethod:Instead of invoking the methods
calcBagel(), calcCoffee(), calcToppings()on the panels that are displayed, you are creating new panels out of nowhere and invoking “calc-methods” on them. Of course they are different objects of the one the user is manipulating in the UI. You should keep a reference to the panels you originally added to your GUI and invoke “calc-methods” on those, not on newly created objects.P.S: Your code is really mixing model and views.