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 7568259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:52:45+00:00 2026-05-30T14:52:45+00:00

My program compiles and runs, but here is my problem. I have a checkbox

  • 0

My program compiles and runs, but here is my problem. I have a checkbox set up for each item, but I only need it to total the items that are checked and not all of the items. Instead its totalling all items regardless of whether or not they are checked.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;

public class StudentShopping extends JFrame
{
    public static void main(String[] args) {

        JFrame frame = new JFrame();

        frame.setSize(550, 400); //Sets size of the window
        frame.setTitle("Student Shopping");//Adds title to the GUI
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JCheckBox ClothingCheckBox = new JCheckBox();
        JLabel Clothinglbl = new JLabel("Clothing");
        final JTextField ClothingField = new JTextField(3);
        ClothingField.setText("0");

        JCheckBox BooksCheckBox = new JCheckBox(); 
        JLabel Bookslbl = new JLabel("Books");
        final JTextField BooksField = new JTextField(3);
        BooksField.setText("0");

        JCheckBox SuppliesCheckBox = new JCheckBox();
        JLabel Supplieslbl = new JLabel("Supplies");
        final JTextField SuppliesField = new JTextField(3);
        SuppliesField.setText("0");

        JCheckBox MealPlanCheckBox = new JCheckBox();
        JLabel MealPlanlbl = new JLabel("MealPlan");
        final JTextField MealPlanField = new JTextField(3);
        MealPlanField.setText("0");

        JCheckBox InternetAccessCheckBox = new JCheckBox();
        JLabel InternetAccesslbl = new JLabel("InternetAccess");
        final JTextField InternetAccessField = new JTextField(3);
        InternetAccessField.setText("0");

        final JTextField TotalField = new JTextField(10);
        TotalField.setText("0");


        JLabel ButtonLabel = new JLabel("Press Button for Total");      
        JButton button  = new JButton("Calculate Total");


        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(6,3));
        panel.add(ClothingCheckBox);
        panel.add(Clothinglbl);
        panel.add(ClothingField);
        panel.add(BooksCheckBox);
        panel.add(Bookslbl);
        panel.add(BooksField);
        panel.add(SuppliesCheckBox);    
        panel.add(Supplieslbl);
        panel.add(SuppliesField);
        panel.add(MealPlanCheckBox);
        panel.add(MealPlanlbl);
        panel.add(MealPlanField);
        panel.add(InternetAccessCheckBox);
        panel.add(InternetAccesslbl);
        panel.add(InternetAccessField);
        panel.add(ButtonLabel);
        panel.add(button);
        panel.add(TotalField);
        frame.add(panel);

        class CalculateListener implements ActionListener {


            public void actionPerformed(ActionEvent event) {

            double Clothing = Double.parseDouble(ClothingField.getText());
            double Books = Double.parseDouble(BooksField.getText()) ;
            double Supplies = Double.parseDouble(SuppliesField.getText());
            double MealPlan = Double.parseDouble(MealPlanField.getText());
            double InternetAccess = Double.parseDouble(InternetAccessField.getText());
            double Total = (Clothing+Books+Supplies+MealPlan+InternetAccess)*100

        DecimalFormat df = new DecimalFormat("$#.00");//creates decimal in currency format
        TotalField.setText(df.format(Total)); //
            }
        }

        ActionListener listener = new CalculateListener();
        button.addActionListener(listener);                     

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        }
 }
  • 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-05-30T14:52:46+00:00Added an answer on May 30, 2026 at 2:52 pm

    Because you are never checking for selected state of checkBoxes. It should be something like this:

    double total = 0;
    if(ClothingCheckBox.isSelected() && !ClothingField.getText().isEmpty()) {
        total += Double.parseDouble(ClothingField.getText());
    }
    if(BooksCheckBox.isSelected() && !BooksField.getText().isEmpty()) {
        total += Double.parseDouble(BooksField.getText());
    }
    if(SuppliesCheckBox.isSelected() && !SuppliesField.getText().isEmpty()){
        total += Double.parseDouble(SuppliesField.getText());
    }
    if(MealPlanCheckBox.isSelected() && !MealPlanField.getText().isEmpty()){
        total += Double.parseDouble(MealPlanField.getText());
    }
    if(InternetAccessCheckBox.isSelected() && !InternetAccessField.getText().isEmpty()){            
         total += Double.parseDouble(InternetAccessField.getText());
    }
    total = total * 100;
    DecimalFormat df = new DecimalFormat("$#.00");
    TotalField.setText(df.format(total));
    

    Things I would like to suggest you:

    1. Learn java coding convention and use them.
    2. You should also check for blank values in text fields before parsing them to double. Note that this also doesn’t ensure that value in text field is parsable to Double.
    3. It seems like you want to have only numeric value in textField. For that don’t use JTextField, use JFormattedTextField.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MPI program which compiles and runs, but I would like to
when i run my program it compiles fine but when it runs i get
The following C++ program compiles and runs as expected: #include <stdio.h> int main(int argc,
I have a strange build problem. I have a simple test program that sends
My code compiles and runs fine in Eclipse, but when I try to make
I have an iPhone app that compiles and runs fine in the Simulator on
I have a problem that I tried to ask about previously, but didn't get
My C(++) program, written and compiled using Visual C(++)/Visual Studio, runs fine on my
I want to compile a "Hello World" MS-DOS exe. Not a program that runs
The example program below compiles two in-memory assemblies. The first compilation works fine. The

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.