0

I have a specific question which I am struggling with so far. I'm working on a specific application where the user can choose insurance options through JCheckBoxes.

I have used a ButtonGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). I used regular (single)JCheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, its name and price is displayed in a text field; the HMO costs $200 per month, the PPO costs $600 per month, the dental coverage adds $75 per month, and the vision care adds $20 per month. When a user deselects an item, the text field is made blank. Please see the code I wrote below.

public class JInsurance extends JFrame implements ItemListener{
private JTextField t1;
private JRadioButton hmo, ppo;
private JCheckBox dental, visual;
private JLabel lbl;
private ButtonGroup grp;
public double total;

public JInsurance(){
    setBounds(100, 100, 450, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(null);

    grp = new ButtonGroup();


    hmo = new JRadioButton("HMO");
    hmo.setBounds(21, 53, 109, 23);
    hmo.setActionCommand("hmo");
    grp.add(hmo);
    add(hmo);

    ppo = new JRadioButton("PPO");
    ppo.setBounds(21, 101, 109, 23);
    ppo.setActionCommand("ppo");
    grp.add(ppo);
    add(ppo);


    dental = new JCheckBox("Dental Insurance");
    dental.setBounds(178, 53, 135, 23);
    add(dental);

    visual = new JCheckBox("Visual Insurance");
    visual.setBounds(178, 101, 118, 23);
    add(visual);

    lbl = new JLabel("You have chosen");
    lbl.setFont(new Font("Tahoma", Font.BOLD, 12));
    lbl.setBounds(41, 193, 109, 23);
    add(lbl);

    t1 = new JTextField();
    t1.setBounds(152, 181, 179, 45);
    add(t1);
    t1.setColumns(10);

    hmo.addItemListener(this);
    ppo.addItemListener(this);
    dental.addItemListener(this);
    visual.addItemListener(this);
}

public void itemStateChanged(ItemEvent e){


    if (hmo.isSelected())
        if(dental.isSelected())
            total = 200 + 75;
        else if(dental.isSelected() && visual.isSelected())
            total = 200 + 75 + 20;
        else if(visual.isSelected())
            total = 200 + 20;
    t1.setText(String.valueOf(total));


         if(ppo.equals("PPO"))
        if(dental.isSelected())
            total = 600 + 75;
        else if(dental.isSelected() && visual.isSelected())
            total = 600 + 75 + 20;
        else if(visual.isSelected())
            total = 600 + 20;
        t1.setText(String.valueOf(total));


    }
UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33

1 Answers1

0

The main issue is that you weren't able to implement the logic properly in the itemStateChanged() method. The following code should solve your problem.

public void itemStateChanged(ItemEvent e){
    if (hmo.isSelected()) {
        total = 200;
        if(dental.isSelected()) {
            total += 75;
        }
        if(visual.isSelected()) {        
            total += 20;
        }
        t1.setText(String.valueOf(total));
    }
    else if(ppo.isSelected()) {
        total = 600;
        if(dental.isSelected()) {
            total += 75;
        }
        if(visual.isSelected()) {
            total += 20;
        }
        t1.setText(String.valueOf(total));
    }
    else {    // In case you later use grp.clearSelection();
        total = 0;
        t1.setText("");
    }
}
UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33
  • Hmm ! I forgot the **+=** ! When a user deselects an item, make the text field blank. ? How does that work ? – Nilesh Bhunjun Mar 14 '15 at 08:37
  • Since you have added the itemListener to all the radio buttons and checkboxes, the method **itemStateChanged()** is getting called each time you select or deselect any of them. – UrsinusTheStrong Mar 14 '15 at 08:41
  • No. I mean when I deselect any of the item, the textfield should be blank. Isn't that possible in itemStateChanged() ? – Nilesh Bhunjun Mar 14 '15 at 08:43
  • This code will work for all cases but since a radiobutton cannot directly be deselected, the last value tends to stick around. You can deselect all radiobuttons under a buttongroup by using **buttonGroup.clearSelection()**. – UrsinusTheStrong Mar 14 '15 at 08:46