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

  • Home
  • SEARCH
  • 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 8883443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:48:49+00:00 2026-06-14T20:48:49+00:00

I’ve created a basic program that uses radio buttons, buttons and check boxes. My

  • 0

I’ve created a basic program that uses radio buttons, buttons and check boxes. My problem is that in runtime the check boxes can be selected but not deselected. I’m pretty sure its something simple but I’ve tried snippets of codes from oracle’s website and it didn’t seem to work. Could any point me to the right direction? Thanks.

This is an inner class that handles the events for the check boxes. It’s suppose to receive the event and set the variables to the appropriate prices. I’ve tried elsebut that doesn’t seem to work.

private void buildPanel()
{
    //Create the label, radio buttons, check boxes, and buttons
    messageLabel = new JLabel("Select the package of your choice: ");
    min1 = new JRadioButton("300 Minutes - $45.00 per month.");
    min2 = new JRadioButton("800 Minutes - $65.00 per month.");
    min3 = new JRadioButton("1500 Minutes -  $99.00 per month.");

    model1 = new JRadioButton("Model 100 - $29.95");
    model2 = new JRadioButton("Model 110 - $49.95 ");
    model3 = new JRadioButton("Model 200 - $99.95");

    vMail = new JCheckBox("Voice Mail  - $5.00 per month.");
    vMail.setSelected(false);
    textMes = new JCheckBox("Text Messaging - $10.00 per month.");
    textMes.setSelected(false);

    okButton = new Button("OK");

    //Group the radio buttons
    radiogroupmin = new ButtonGroup();
    radiogroupmin.add(min1);
    radiogroupmin.add(min2);
    radiogroupmin.add(min3);

    radiogroupmodel = new ButtonGroup();
    radiogroupmodel.add(model1);
    radiogroupmodel.add(model2);
    radiogroupmodel.add(model3);

    //Group the check boxes
    checkboxgroup = new ButtonGroup();

    checkboxgroup.add(vMail);
    checkboxgroup.add(textMes);

    //Add action listener to the radio buttons
    min1.addActionListener(new RadioButtonListener());
    min2.addActionListener(new RadioButtonListener());
    min3.addActionListener(new RadioButtonListener());

    model1.addActionListener(new RadioButtonListener());
    model2.addActionListener(new RadioButtonListener());
    model3.addActionListener(new RadioButtonListener());

    //Add action listener to the check boxes
    vMail.addItemListener(new CheckBoxListener());
    textMes.addItemListener(new CheckBoxListener());

    //Add action listener to the button
    okButton.addActionListener(new ButtonListener());

    //create a panel and add the components to it
    panel = new JPanel(new BorderLayout());
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

    panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    panel.add(messageLabel);
    panel.add(min1);
    panel.add(min2);
    panel.add(min3);


    panel.add(model1);
    panel.add(model2);
    panel.add(model3);


    panel.add(vMail);
    panel.add(textMes);     

    panel.add(okButton);
}

public static class RadioButtonListener implements ActionListener
{       
        public static double minPrice;
        public static String model;
        public static double modelPrice = 0.0;

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == min1)
        {
            minPrice = 45.00;
        }
        else if(e.getSource() == min2)
        {
            minPrice = 65.00;
        }
        else if(e.getSource() == min3)
        {
            minPrice = 99.00;
        }

        if(e.getSource() == model1)
        {
            model = "Model 100";
            modelPrice = 29.00;
        }
        else if(e.getSource() == model2)
        {
            model = "Model 110";
            modelPrice = 49.99;
        }
        else if(e.getSource() == model3)
        {
            model = "Model 200";
            modelPrice = 99.99;
        }
    }


    public static double getMinPrice()
    {
        return minPrice;
    }

    public static  String getModel()
    {
        return model;
    }

    public static double getModelPrice()
    {
        return modelPrice;
    }
}

public static class CheckBoxListener implements ItemListener
{

    static double voicePrice;
    static double  textPrice;
    public void itemStateChanged(ItemEvent e) 
    {
        if(e.getSource() == vMail)
        {
            if (e.getStateChange() == ItemEvent.SELECTED)
            {
            voicePrice = 5.00;
            }
            else
            {
            voicePrice = 0.0;
            }
        }           

        if(e.getSource() == textMes)
        {
            if(e.getStateChange() == ItemEvent.SELECTED)
            {
                textPrice = 10.00;
            }
            else
            {
                textPrice = 0.0;
            }
        }
    }

    public static double getVoicePrice()
    {
        return voicePrice;
    }

    public static double getTextPrice()
    {
        return textPrice;
    }   

}
private  class ButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == okButton)
        {
            DecimalFormat df = new DecimalFormat("$00.##");

            double total;
            final double tax = 0.6;
            final double modelTax = (RadioButtonListener.getModelPrice() * tax) + RadioButtonListener.getModelPrice();
            total = RadioButtonListener.getMinPrice() + 
                    modelTax + CheckBoxListener.getVoicePrice() + CheckBoxListener.getTextPrice(); 
            JOptionPane.showMessageDialog(null, "Your total is: " + df.format(total));

        }
    }
}
  • 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-14T20:48:50+00:00Added an answer on June 14, 2026 at 8:48 pm

    Perhaps try changing both:

    if(vMail.isSelected())
    

    and:

    if(textMes.isSelected())
    

    to:

    if(e.getStateChange() == ItemEvent.SELECTED)
    

    Revised answer below based on the new info

    Okay, there are a few problems:

    1. Remove all code related to the checkboxgroup. You do not want to use a ButtonGroup from checkboxes! This is your main problem. The ButtonGroup is only generally used for JRadioButtons
    2. Only create the one instance of CheckBoxListener, ie:
    
        // Add action listener to the check boxes
        CheckBoxListener checkBoxListener = new CheckBoxListener();
        vMail.addItemListener(checkBoxListener);
        textMes.addItemListener(checkBoxListener);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am currently running into a problem where an element is coming back from

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.