-1

I want in my program 8 checkboxes, and each one of them if its clicked there will be statements It works nice with the first two checkboxes, but starting from the 3rd one i faced problem when I click the 3rd one for example it didn't show show the statemnts.

Can anyone tell me where is the problem please. Here is the code :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Satis extends JFrame implements ActionListener
{
JCheckBox b1,b2,b3,b4,b5,b6,b7,b8;

  JLabel la2,la22,la222,la3,la33,la333,la4,la44,la444,la5,la55,la555,la6,la66,la666,la7,la77,la777,la8,la88,la888,la9,la99,la999;
public Satis()
{
     this.setLayout(null);

      JLabel la1=new JLabel("Select your problem area to get an advice:");
      la1.setBounds(160,20,340,20);
       la2=new JLabel("");
       la22=new JLabel("");
       la222=new JLabel("");
       la3=new JLabel("");
       la33=new JLabel("");
       la333=new JLabel("");
       la4=new JLabel("");
       la44=new JLabel("");
       la444=new JLabel("");
       la5=new JLabel("");
       la55=new JLabel("");
       la555=new JLabel("");
       la6=new JLabel("");
       la66=new JLabel("");
       la666=new JLabel("");
       la7=new JLabel("");
       la77=new JLabel("");
       la777=new JLabel("");
       la8=new JLabel("");
       la88=new JLabel("");
       la888=new JLabel("");
       la9=new JLabel("");
       la99=new JLabel("");
       la999=new JLabel("");


      b1=new JCheckBox("Housing");
      b1.setBounds(20,45,120,20);
      b2=new JCheckBox("Medical");
      b2.setBounds(440,45,120,20);
      b3=new JCheckBox("Transportation");
      b3.setBounds(20,120,140,20);
      b4=new JCheckBox("Food");
      b4.setBounds(440,120,140,20);
      b5=new JCheckBox("Utility");
      b6=new JCheckBox("Clothes");
      b7=new JCheckBox("Debt payments");
      b8=new JCheckBox("Personal");

      add(la1);
      add(la2);
      add(la22);
      add(la222);
      la2.setBounds(10,60,330,20);
      la22.setBounds(10,75,330,20);
      la222.setBounds(10,90,330,20);
      add(b1);

      add(la3);
      add(la33);
      add(la333);
      la3.setBounds(300,60,330,20);
      la33.setBounds(300,75,330,20);
      la333.setBounds(300,90,330,20);
      add(b2);

      add(la4);
      add(la44);
      add(la444);
      la4.setBounds(10,135,330,20);
      la44.setBounds(10,150,330,20);
      la444.setBounds(10,165,330,20);
      add(b3);

      add(la5);
      add(la55);
      add(la555);
      la5.setBounds(330,135,330,20);
      la5.setBounds(330,150,330,20);
      la5.setBounds(330,165,330,20);

      add(b4);

      add(la6);
      add(b5);

      add(la7);
      add(b6);

      add(la8);
      add(b7);

      add(la9);
      add(b8);


      b1.addActionListener(this);
      b2.addActionListener(this);
}
public void actionPerformed (ActionEvent ae)
{
    String s=ae.getActionCommand();
    if (b1.isSelected())
    {
        la2.setText("1. Move to a smaller place "); 
        la22.setText("2. Move further from the city");
        la222.setText("3. Raise your insurance deductible");
    }else {
        la2.setText("");
        la22.setText("");
        la222.setText("");

    }


    if (b2.isSelected())
        {
            la3.setText("1. Dont procrastinate");
            la33.setText("2. Reevaluate your vitamins");
            la333.setText("3. Ask: Do I really need that test?");
        }
            else {
                la3.setText("");
    la33.setText("");
    la333.setText("");
            }

if (b3.isSelected())
    {

        la4.setText("1. Calm your driving habits");
        la44.setText("2. Take advantage of car insurance discounts");
        la444.setText("3. Look for local transportation incentives");
    }
        else {
            la4.setText("");
la44.setText("");
la444.setText("");
        }
    if (b4.isSelected())
    {
        la5.setText("1. Make Healthy Choices, they’re Cheaper ");
        la55.setText("2. Use Sales and Coupons");
        la555.setText("3. Plant a Garden");
    }
        else {
            la5.setText("");
            la55.setText("");
            la555.setText("");
        }

    if (b5.isSelected())
    {
        la6.setText("1. Buy Energy-Efficient Appliances ");
        la66.setText("2. Scale Back on Cable Channels");
        la666.setText("3. Fix Leaks");
    }
        else {
            la6.setText("");
            la66.setText("");
            la666.setText("");
        }
    if (b6.isSelected())
    {
        la7.setText("1. Buy Better Quality Clothing ");
        la77.setText("2. Buy Clothes at the Right Time");
        la777.setText("3. Take Care of Your Clothes Better");
    }
        else {
            la7.setText("");
            la77.setText("");
            la777.setText("");
        }
    if (b7.isSelected())
    {
        la8.setText("1. Track Spending ");
        la88.setText("2. Build Savings");
        la888.setText("3. Reduce Debt");
    }
        else {
            la8.setText("");
            la88.setText("");
            la888.setText("");
        }
    if (b8.isSelected())
    {
        la9.setText("1. Avoid Full-Price Textbooks ");
        la99.setText("2. Get deals");
        la999.setText("3. Shop online");
    }
        else {
            la9.setText("");
            la99.setText("");
            la999.setText("");
        }






    }
public static void main(String[] args)
{
    Satis St=new Satis();
    St.setVisible(true);
    St.setSize(600,400);
}

}
Najla
  • 1
  • Your first two JCheckBoxes have appropriate listeners added, so you already know how to do this -- why are you asking this question then? – Hovercraft Full Of Eels Dec 11 '16 at 16:49
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Note that an MCVE would have 3 check boxes rather than 8. – Andrew Thompson Dec 11 '16 at 23:21

1 Answers1

0

You forgot to add

   b3.addActionListener(this);

So the method is never call when you use the JCheckBox.

Benoît Verhaeghe
  • 612
  • 1
  • 6
  • 21