Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:27:18+00:00 2026-06-03T12:27:18+00:00

I’m new here, and this is my very first post. I have a very

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:27:19+00:00Added an answer on June 3, 2026 at 12:27 pm

    Your problem is right here, in your actionPerformed method:

        double total = 0.0;
        bagels = new BagelPanel();
        coffee = new CoffeePanel();
        toppings = new ToppingsPanel();
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.