JFrame, JCheckbox, Input/Output, Swing Components

I'm having trouble getting my JFrame to show when I run my program it just shows me a blank frame. I have my Checkboxes declared but nothing is showing. Help me please JMyNewHome.java
lovemycomputersAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HonorGodSoftware EngineerCommented:
Move this statement:

      aFrame.setVisible(true);

to after you have added the components...

Perhaps something like this:


import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
public class JMyNewHome extends JFrame 
{
    public static void main(String[] args)
    {
        final int Aspen = 100000;
        final int Brittany = 120000;
        final int Colonial = 180000;
        final int Dartmoor = 250000;
    
        JFrame aFrame = new JFrame("My New Home");

        aFrame.setSize(350, 150);
        String[] models = { "Aspen", "Brittany", "Colonial", "Dartmoor" };
    
        JCheckBox chk1 = new JCheckBox(models[0], true);
        JCheckBox chk2 = new JCheckBox(models[1], false);
        JCheckBox chk3 = new JCheckBox(models[2], false);
        JCheckBox chk4 = new JCheckBox(models[3], false);
        aframe.add( chk1 );
        aframe.add( chk2 );
        aframe.add( chk3 );
        aframe.add( chk4 );

        String[] bed = { "2 bedrooms", "3 bedrooms", "4 bedrooms" };
        JCheckBox chk5 = new JCheckBox(bed[0],false);
        JCheckBox chk6 = new JCheckBox(bed[1],true);
        JCheckBox chk7 = new JCheckBox(bed[2],false);
        aframe.add( chk5 );
        aframe.add( chk6 );
        aframe.add( chk7 );

        String[] garage = { "no garage", "1 car", "2 car", "3 car" };
        JCheckBox chk8  = new JCheckBox(garage[0],false);
        JCheckBox chk9  = new JCheckBox(garage[1],true);
        JCheckBox chk10 = new JCheckBox(garage[2],false);
        JCheckBox chk11 = new JCheckBox(garage[3],false);
        aframe.add( chk8 );
        aframe.add( chk9 );
        aframe.add( chk10 );
        aframe.add( chk11 );

        aFrame.setVisible(true);
    }
}

Open in new window

HonorGodSoftware EngineerCommented:
This works better... but it is only a start:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JCheckBox;

public class JMyNewHome
 {
    public static void main( String[] args )
    {
        final int Aspen    = 100000;
        final int Brittany = 120000;
        final int Colonial = 180000;
        final int Dartmoor = 250000;
    
        JFrame aFrame = new JFrame("My New Home");

        aFrame.setSize(350, 150);
        aFrame.setLayout( new FlowLayout() );
        
        String[] models = { "Aspen", "Brittany", "Colonial", "Dartmoor" };

        JCheckBox chk1 = new JCheckBox( models[ 0 ], true  );
        JCheckBox chk2 = new JCheckBox( models[ 1 ], false );
        JCheckBox chk3 = new JCheckBox( models[ 2 ], false );
        JCheckBox chk4 = new JCheckBox( models[ 3 ], false );

        aFrame.add( chk1 );
        aFrame.add( chk2 );
        aFrame.add( chk3 );
        aFrame.add( chk4 );

//      String[] bed = { "2 bedrooms", "3 bedrooms", "4 bedrooms" };
//      JCheckBox chk5 = new JCheckBox(bed[0],false);
//      JCheckBox chk6 = new JCheckBox(bed[1],true);
//      JCheckBox chk7 = new JCheckBox(bed[2],false);
//      aFrame.add( chk5 );
//      aFrame.add( chk6 );
//      aFrame.add( chk7 );

//      String[] garage = { "no garage", "1 car", "2 car", "3 car" };
//      JCheckBox chk8  = new JCheckBox(garage[0],false);
//      JCheckBox chk9  = new JCheckBox(garage[1],true);
//      JCheckBox chk10 = new JCheckBox(garage[2],false);
//      JCheckBox chk11 = new JCheckBox(garage[3],false);
//      aFrame.add( chk8 );
//      aFrame.add( chk9 );
//      aFrame.add( chk10 );
//      aFrame.add( chk11 );

        aFrame.setVisible( true );
    }
}

Open in new window



My-New-Home.png
HonorGodSoftware EngineerCommented:
One thing I like to do is start the Application in the middle of the screen, which is as easy as adding:

aFrame.setLocationRelativeTo( null );

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JCheckBox;

public class JMyNewHome
 {
    public static void main( String[] args )
    {
        final int Aspen    = 100000;
        final int Brittany = 120000;
        final int Colonial = 180000;
        final int Dartmoor = 250000;
    
        JFrame aFrame = new JFrame("My New Home");

        aFrame.setSize(350, 150);
        aFrame.setLayout( new FlowLayout() );
        aFrame.setLocationRelativeTo( null );
        
        String[] models = { "Aspen", "Brittany", "Colonial", "Dartmoor" };

        JCheckBox chk1 = new JCheckBox( models[ 0 ], true  );
        JCheckBox chk2 = new JCheckBox( models[ 1 ], false );
        JCheckBox chk3 = new JCheckBox( models[ 2 ], false );
        JCheckBox chk4 = new JCheckBox( models[ 3 ], false );

        aFrame.add( chk1 );
        aFrame.add( chk2 );
        aFrame.add( chk3 );
        aFrame.add( chk4 );

        String[] bed = { "2 bedrooms", "3 bedrooms", "4 bedrooms" };
        JCheckBox chk5 = new JCheckBox(bed[0],false);
        JCheckBox chk6 = new JCheckBox(bed[1],true);
        JCheckBox chk7 = new JCheckBox(bed[2],false);
        aFrame.add( chk5 );
        aFrame.add( chk6 );
        aFrame.add( chk7 );

        String[] garage = { "no garage", "1 car", "2 car", "3 car" };
        JCheckBox chk8  = new JCheckBox(garage[0],false);
        JCheckBox chk9  = new JCheckBox(garage[1],true);
        JCheckBox chk10 = new JCheckBox(garage[2],false);
        JCheckBox chk11 = new JCheckBox(garage[3],false);
        aFrame.add( chk8 );
        aFrame.add( chk9 );
        aFrame.add( chk10 );
        aFrame.add( chk11 );

        aFrame.setVisible( true );
    }
}

Open in new window

My-New-Home.png
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

HonorGodSoftware EngineerCommented:
Another thing that people like to do is have the JVM terminate when the close button is clicked by adding:

aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JCheckBox;

public class JMyNewHome
 {
    public static void main( String[] args )
    {
        final int Aspen    = 100000;
        final int Brittany = 120000;
        final int Colonial = 180000;
        final int Dartmoor = 250000;
    
        JFrame aFrame = new JFrame("My New Home");

        aFrame.setSize(350, 150);
        aFrame.setLayout( new FlowLayout() );
        aFrame.setLocationRelativeTo( null );
        aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        String[] models = { "Aspen", "Brittany", "Colonial", "Dartmoor" };

        JCheckBox chk1 = new JCheckBox( models[ 0 ], true  );
        JCheckBox chk2 = new JCheckBox( models[ 1 ], false );
        JCheckBox chk3 = new JCheckBox( models[ 2 ], false );
        JCheckBox chk4 = new JCheckBox( models[ 3 ], false );

        aFrame.add( chk1 );
        aFrame.add( chk2 );
        aFrame.add( chk3 );
        aFrame.add( chk4 );

        String[] bed = { "2 bedrooms", "3 bedrooms", "4 bedrooms" };
        JCheckBox chk5 = new JCheckBox(bed[0],false);
        JCheckBox chk6 = new JCheckBox(bed[1],true);
        JCheckBox chk7 = new JCheckBox(bed[2],false);
        aFrame.add( chk5 );
        aFrame.add( chk6 );
        aFrame.add( chk7 );

        String[] garage = { "no garage", "1 car", "2 car", "3 car" };
        JCheckBox chk8  = new JCheckBox(garage[0],false);
        JCheckBox chk9  = new JCheckBox(garage[1],true);
        JCheckBox chk10 = new JCheckBox(garage[2],false);
        JCheckBox chk11 = new JCheckBox(garage[3],false);
        aFrame.add( chk8 );
        aFrame.add( chk9 );
        aFrame.add( chk10 );
        aFrame.add( chk11 );

        aFrame.setVisible( true );
    }
}

Open in new window

lovemycomputersAuthor Commented:
Oh ok I actually understand that lol. I have to build a new house and it has to show the final price in a textbox. I have the textbox but I'm not sure how to get the price. The user is to select the model of the home: For an Aspen its $100,000, Brittany is $120,000, Colonial $180,000, and a Dartmoor is $250,000. They can choose 2bedrooms, 3bedrooms or 4bedrooms and when they choose a bedroom it adds $10,500 to the base price. They then choose the number of garages either no car, 1 car or 3 car and it adds $7,775 to the base price. I know I have to add an ItemListener but its kind of hard for me to do correctly. This would be simple if i could do it in Visual Basic but I cant can you give me a little information on how to do this. I don't want the work done for me I want to learn it.
JMyNewHome.java
zzynxSr. Software engineerCommented:
>> I don't want the work done for me I want to learn it.
Since this looks like a home work question, we're not allowed to write the code for you neither.

If you read the paragraphs "How to use check boxes" and "How to use radio buttons" @ http://docs.oracle.com/javase/tutorial/uiswing/components/button.html
you should have enough information to write the code you need to.
HonorGodSoftware EngineerCommented:
> I have the textbox but I'm not sure how to get the price.

  From the text box?

- Take a look at the "How to Use Text Fields" section of the Java Swing Tutorials:
  http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html
zzynxSr. Software engineerCommented:
So, you'll have three groups of radio boxes:
- one for the model type containing four radio buttons ("Aspen", "Brittany", "Colonial" and "Dartmoor")
- one for the number of bed rooms containing three radio buttons ("2 bed rooms", "3 bed rooms" and "4 bed rooms")
- one for the number of garages containing three radio buttons ("no garage", "garage for 1 car", "garage for 3 cars")

Use a variable (eg. 'finalPrice') to store the final price in

Now for each radio button change (your action listener detects), you have to recalculate the final price.

* You start with initializing the finalPrice:
          finalPrice = 0;
* Then you add 100,000, 120,000, 180,000 or 250,000 depending on which model type is selected.
* Then you add 21,000, 31,500 or 42,000 depending on the number of bed rooms that are selected
* Then you add 0, 7,775 or 15550  depending on the garage type selection
* At last you display that final price in you textbox:  
         myFinalPriceTextField.setText( finalPrice + "");

lovemycomputersAuthor Commented:
I'm getting these errors when I compile after I try to add ActionListeners to my code
JMyNewHome.java:44: non-static variable this cannot be referenced from a static context
      twobedrooms.addActionListener(this);
                                    ^
JMyNewHome.java:44: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (JMyNewHome)
      twobedrooms.addActionListener(this);
                 ^
2 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
Whats going on
zzynxSr. Software engineerCommented:
Don't write all your program logic in your main method

Better do somehting like this:

public class JMyNewHome extends JFrame {

  public JMyNewHome(String title) {
     super(title);
     ... // Do here all your layouting/content stuff you previously did inside the main method
  }

  public static void main (String[] arguments) {
     JMyNewHome frame = new JMyNewHome("My New Home");
     frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     frame.setSize(350, 150);
     frame.setVisible(true);
  }

}

Open in new window

lovemycomputersAuthor Commented:
Ok I give up its not working out for me I don't understand Java. Thanks for at least attempting to help me sir. Have a good day.
zzynxSr. Software engineerCommented:
>> Ok I give up
That's a pity to hear.
>> I don't understand Java
Now maybe you don't yet. But I'm sure if you keep trying it would come.
Sure you're giving up not too early?

Just move the stuff out of your (static) main method to the constructor method I gave you and it should work.
From there on I'll help you further. You're not that far away from the solution...
Whatever you decide: success.
HonorGodSoftware EngineerCommented:
There is little doubt that trying to wrap your head around event driven programming can be a challenge!

There are lots of things going on.

If you are interested in learning Java Swing, I can highly recommend:

Swing: A Beginner's Guide
     By: Herbert Schildt
  ISBN: 0072263148
Amazon: www.amazon.com/dp/0072263148

  His writing style is good, and he does a good job of explaining things.
The code samples are supposed to be available from the publisher website: http://www.osborne.com
lovemycomputersAuthor Commented:
No not giving up completely just for a few days. I think I may be trying too hard right now. I'm going to let my mind rest for a while before trying to comprehend again you guys. Thanks for the concern though.
lovemycomputersAuthor Commented:
Ok now I added the ItemListeners to my code but now I get these 2 errors when I compile. I put the JMyNewHome method in just like the book tells me but its still returning errors..??? JMyNewHome.java
HonorGodSoftware EngineerCommented:
What errors are occurring?

When?

  - During compilation?
  - During execution?

Suggestion:
  Start simple
  Get clean compilations (i.e., not errors, or warnings)
  Add one thing at a time, recompile, fix warning & errors

First step:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMyNewHome extends JFrame implements ItemListener
{
    public static void main( String[] args )
    {
        JFrame home = new JFrame( "My New Home" );
        home.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        home.setVisible( true );
    }

    public void itemStateChanged(ItemEvent e) {
    }
}

Open in new window

HonorGodSoftware EngineerCommented:
Next iteration:
- 2 "simple" statements added
- clean compilation

 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMyNewHome extends JFrame implements ItemListener
{
    public static void main( String[] args )
    {
        JFrame home = new JFrame( "My New Home" );
        home.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        home.setSize( 500, 500 );
        home.setLocationRelativeTo( null );
        home.setVisible( true );
    }

    public void itemStateChanged(ItemEvent e) {
    }
}

Open in new window

zzynxSr. Software engineerCommented:
>> I get these 2 errors when I compile.
Please, please, please. Post the EXACT errors you get (including the line numbers where they occur)
zzynxSr. Software engineerCommented:
All right, some remarks:
1) Since you declared your class JMyNewHome as implementing the interface ItemListener, your class should have a method like this:

    public void itemStateChanged(ItemEvent e) {
    }

   But I think it's better to implement the ActionListener interface, since you want to listen to checkboxes that are selected by the user.
   ItemChangListener is to listen for changes in the selection of list items. (like eg. in a JList)

   So to implement ActionListener, you need to have a method

   public void actionPerformed(ActionEvent e) {
   }

2) The method
 
    public static void main(String[] args)

is the entry point of your program. But it shouldn't contain the constructor (method) of your class.
That constructor method
          public JMyNewHome()
should be inside your class, but not inside the main() method

3) Variable "twobox" doesn't exist. You defined its name as "twoBox", so you have to use the same name in the constructor.

4) When you correct the above, recompile and run the program you won't see anything yet, since in the constructor method you don't add the controls to the content panel of your frame. You just define them, but don't add them. I corrected that using "some" layout (that you can adapt later on to your own taste).

If I apply all the above to your code, I get the code like you see beneath.
Now compile this code and run it.
Then change the home model by selecting another model and see what happens.

You see that the magic is in that actionPerformed() method.
Of course, you have to output the price and not a text, but I did that just to demonstrate the principle.
I think it should be more or less straightforward to change the actionPerformed() method to output the price whenever another selection is made.

If you have any questions about this code let me know.
package com.energyict.mdwswing.core;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMyNewHome extends JFrame implements ActionListener
{
    String companyName = new String("Carlisle Construction");
    Font bigFont = new Font("Arial", Font.PLAIN, 24);
    final int APRICE = 100000, BPRICE = 120000, CPRICE = 180000, dPrice = 250000;
    int totalPrice=0;
    ButtonGroup modelGrp = new ButtonGroup();
    JCheckBox aBox = new JCheckBox("Aspen",false);
    JCheckBox bBox = new JCheckBox("Brittany", false);
    JCheckBox cBox = new JCheckBox("Colonial", false);
    JCheckBox dBox = new JCheckBox("Dartmoor", false);
    final int TWOPRICE = 21000, THREEPRICE = 31500, FOURPRICE = 42000;
    ButtonGroup bdrmGrp = new ButtonGroup();
    JCheckBox twoBox = new JCheckBox("2 bedrooms", false);
    JCheckBox threeBox = new JCheckBox("3 bedrooms", false);
    JCheckBox fourBox = new JCheckBox("4 bedrooms", false);
    final int NOCARPRICE = 0, ONECARPRICE = 7775, TWOCARPRICE = 15550, THREECARPRICE = 23325;
    ButtonGroup garGrp = new ButtonGroup();
    JCheckBox noCarBox = new JCheckBox("no garage",false);
    JCheckBox oneCarBox = new JCheckBox("1 car",false);
    JCheckBox twoCarBox = new JCheckBox("2 car",false);
    JCheckBox threeCarBox = new JCheckBox("3 car",false);
    JLabel totPrice = new JLabel();
    String output;

    public JMyNewHome() {
        super("My New Home");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(0,1));
        modelGrp.add(aBox);
        aBox.addActionListener(this);
        modelGrp.add(bBox);
        bBox.addActionListener(this);
        modelGrp.add(cBox);
        cBox.addActionListener(this);
        modelGrp.add(dBox);
        dBox.addActionListener(this);
        bdrmGrp.add(twoBox);
        twoBox.addActionListener(this);
        bdrmGrp.add(threeBox);
        threeBox.addActionListener(this);
        bdrmGrp.add(fourBox);
        fourBox.addActionListener(this);
        garGrp.add(noCarBox);
        noCarBox.addActionListener(this);
        garGrp.add(oneCarBox);
        oneCarBox.addActionListener(this);
        garGrp.add(twoCarBox);
        twoCarBox.addActionListener(this);
        garGrp.add(threeCarBox);
        threeCarBox.addActionListener(this);

        JPanel typePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        typePanel.setBorder(BorderFactory.createTitledBorder("Home model"));
        typePanel.add(aBox);
        aBox.setSelected(true); // select the first option by default
        typePanel.add(bBox);
        typePanel.add(cBox);
        typePanel.add(dBox);
        add(typePanel);

        JPanel bedroomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        bedroomPanel.setBorder(BorderFactory.createTitledBorder("Number of bed rooms"));
        bedroomPanel.add(twoBox);
        twoBox.setSelected(true); // select the first option by default
        bedroomPanel.add(threeBox);
        bedroomPanel.add(fourBox);
        add(bedroomPanel);

        JPanel garagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        garagePanel.setBorder(BorderFactory.createTitledBorder("Garage type"));
        garagePanel.add(noCarBox);
        noCarBox.setSelected(true); // select the first option by default
        garagePanel.add(oneCarBox);
        garagePanel.add(twoCarBox);
        garagePanel.add(threeCarBox);
        add(garagePanel);

        JPanel pricePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pricePanel.setBorder(BorderFactory.createTitledBorder(""));
        pricePanel.add(new JLabel("Total price: "));
        totPrice.setText("home model Aspen is chosen"); // by default
        pricePanel.add(totPrice);
        add(pricePanel);

        pack(); // Let the layout manager "auto-layout" all the above
    }

    public static void main(String[] args) {
        JMyNewHome frame = new JMyNewHome();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public void actionPerformed(ActionEvent e) {
        if (aBox.isSelected()) {
            totPrice.setText("home model Aspen is chosen");
        } else if (bBox.isSelected()) {
            totPrice.setText("home model Brittany is chosen");
        }  else if (cBox.isSelected()) {
            totPrice.setText("home model Colonial is chosen");
        }  else if (dBox.isSelected()) {
            totPrice.setText("home model Dartmoor is chosen");
        }
        // You can do the same for the other check boxes
    }
}

Open in new window

zzynxSr. Software engineerCommented:
Correction: Remove the first line (that package statement) of the code I posted! You don't need it and it will give errors.
lovemycomputersAuthor Commented:
Z now none of my CheckBoxes are displaying when I run the program.I have it set to visible already and when i compile there isnt an error in sight.

import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

      public class JMyNewHome extends JFrame implements ActionListener
{
      int APRICE = 100000, BPRICE = 120000, CPRICE = 180000, dPrice = 250000;
      ButtonGroup modelGrp = new ButtonGroup();
      JCheckBox aBox = new JCheckBox("Aspen",false);
      JCheckBox bBox = new JCheckBox("Brittany", false);
      JCheckBox cBox = new JCheckBox("Colonial", false);
      JCheckBox dBox = new JCheckBox("Dartmoor", false);
      final int TWOPRICE = 21000, THREEPRICE = 31500, FOURPRICE = 42000;
      ButtonGroup bdrmGrp = new ButtonGroup();
      JCheckBox twoBox = new JCheckBox("2 bedrooms", false);
      JCheckBox threeBox = new JCheckBox("3 bedrooms", false);
      JCheckBox fourBox = new JCheckBox("4 bedrooms", false);
      final int NOCARPRICE = 0, ONECARPRICE = 7775, TWOCARPRICE = 15550, THREECARPRICE = 23325;
      ButtonGroup garGrp = new ButtonGroup();
      JCheckBox noCarBox = new JCheckBox("no garage",false);
      JCheckBox oneCarBox = new JCheckBox("1 car",false);
      JCheckBox twoCarBox = new JCheckBox("2 car",false);
      JCheckBox threeCarBox = new JCheckBox("3 car",false);
      JTextField toPrice = new JTextField(10);

      public JMyNewHome()
{
      super("My New Home");
      modelGrp.add(aBox);
      aBox.addActionListener(this);
      modelGrp.add(bBox);
      bBox.addActionListener(this);
      modelGrp.add(cBox);
      cBox.addActionListener(this);
      modelGrp.add(dBox);
      dBox.addActionListener(this);
      bdrmGrp.add(twoBox);
      twoBox.addActionListener(this);
      bdrmGrp.add(threeBox);
      threeBox.addActionListener(this);
      bdrmGrp.add(fourBox);
      fourBox.addActionListener(this);
      garGrp.add(noCarBox);
      noCarBox.addActionListener(this);
      garGrp.add(oneCarBox);
      oneCarBox.addActionListener(this);
      garGrp.add(twoCarBox);
      twoCarBox.addActionListener(this);
      garGrp.add(threeCarBox);
      threeCarBox.addActionListener(this);
}      
      public static void main(String[] args)
      {
            
      JFrame Frame = new JFrame("My New Home");
      Frame.setSize(400, 200);
      Frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      Frame.setVisible(true);
      Frame.setVisible(true);
      }

      public void actionPerformed(ActionEvent e)
      {
       if (aBox.isSelected()) {
            toPrice.setText("100000");
        } else if (bBox.isSelected()) {
            toPrice.setText("120000");
        }  else if (cBox.isSelected()) {
            toPrice.setText("180000");
        }  else if (dBox.isSelected()) {
            toPrice.setText("250000");
        }
}
}

 Is there like a website you can give me to help me out with this project? I feel like theres not much more to do for this one.
zzynxSr. Software engineerCommented:
>> I feel like theres not much more to do for this one.
Don't agree.

>> now none of my CheckBoxes are displaying when I run the program
Of course not!
The code I posted has this:
JMyNewHome frame = new JMyNewHome();

Open in new window

while yours does this:
JFrame Frame = new JFrame("My New Home");

Open in new window


That's a huge difference. You just create and show an empty JFrame!

You don't run the code I posted.
Please, please, please, compile and run EXACTLY the code I posted.
It wil run OK. Then start making changes one by one.

>> Is there like a website you can give me to help me out with this project?
You defintely need a thourough understanding of the java Swing components and how to use them.
That's why I would recommend http://docs.oracle.com/javase/tutorial/uiswing/components/
HonorGodSoftware EngineerCommented:
Or, as I pointed out earlier http:#a37400501 a good book
lovemycomputersAuthor Commented:
OMG Z Its working!!!!!!!!!!!!!!!!! However when I click the boxes the amounts dont  show in the text field they show  beside it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
      public class JMyNewHome extends JFrame implements ItemListener
{
      FlowLayout flow = new FlowLayout();
      String companyName = new String("Carlisle Construction");
      Font bigFont = new Font("Arial", Font.PLAIN, 24);
      final int APRICE = 100000, BPRICE = 120000, CPRICE = 180000, DPRICE = 250000;
      int totalPrice=0;
      ButtonGroup modelGrp = new ButtonGroup();
      JCheckBox aBox = new JCheckBox("Aspen",false);
      JCheckBox bBox = new JCheckBox("Brittany", false);
      JCheckBox cBox = new JCheckBox("Colonial", false);
      JCheckBox dBox = new JCheckBox("Dartmoor", false);
      final int TWOPRICE = 21000, THREEPRICE = 31500, FOURPRICE = 42000;
      ButtonGroup bdrmGrp = new ButtonGroup();
      JCheckBox twoBox = new JCheckBox("2 bedrooms", false);
      JCheckBox threeBox = new JCheckBox("3 bedrooms", false);       
      JCheckBox fourBox = new JCheckBox("4 bedrooms", false);
      final int NOCARPRICE = 0, ONECARPRICE = 7775, TWOCARPRICE = 15550, THREECARPRICE = 23325;
      ButtonGroup garGrp = new ButtonGroup();
      JCheckBox noCarBox = new JCheckBox("no garage",false);
      JCheckBox oneCarBox = new JCheckBox("1 car",false);
      JCheckBox twoCarBox = new JCheckBox("2 car",false);
      JCheckBox threeCarBox = new JCheckBox("3 car",false);
      JLabel totPrice = new JLabel();
      String output;
      public JMyNewHome()
{
      super("My New Home");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(flow);
      modelGrp.add(aBox);
      aBox.addItemListener(this);
      modelGrp.add(bBox);
      bBox.addItemListener(this);
      modelGrp.add(cBox);
      cBox.addItemListener(this);
      modelGrp.add(dBox);
      dBox.addItemListener(this);
      bdrmGrp.add(twoBox);
      twoBox.addItemListener(this);
      bdrmGrp.add(threeBox);
      threeBox.addItemListener(this);
      bdrmGrp.add(fourBox);
      fourBox.addItemListener(this);
      garGrp.add(noCarBox);
      noCarBox.addItemListener(this);
      garGrp.add(oneCarBox);
      oneCarBox.addItemListener(this);       
      garGrp.add(twoCarBox);
      twoCarBox.addItemListener(this);
      garGrp.add(threeCarBox);
      threeCarBox.addItemListener(this);
      
      JPanel modelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
      modelPanel.add(aBox);
      modelPanel.add(bBox);
      modelPanel.add(cBox);
      modelPanel.add(dBox);
      add(modelPanel);
      
      JPanel bedPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));  
      bedPanel.add(twoBox);
      bedPanel.add(threeBox);
      bedPanel.add(fourBox);
      add(bedPanel);
      
      JPanel garagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
      garagePanel.add(noCarBox);  
      garagePanel.add(oneCarBox);
      garagePanel.add(twoCarBox);
      garagePanel.add(threeCarBox);
      add(garagePanel);
      
      JTextField totalPrice = new JTextField(10);
      add(totalPrice);
      add(totPrice);
      totalPrice.setEditable(false);
      
}

      public static void main(String[] args)
{
      JMyNewHome frame = new JMyNewHome();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 200);
      frame.setLocationRelativeTo( null );
      frame.setVisible( true );
}

      public void itemStateChanged(ItemEvent check)
{
      Object source = check.getItem();
      if(source == aBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += APRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= APRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == bBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += BPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= BPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == cBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += CPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= CPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == dBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += DPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= DPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == twoBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += TWOPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= TWOPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == threeBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += THREEPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= THREEPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == fourBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += FOURPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= FOURPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == noCarBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += NOCARPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= NOCARPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == oneCarBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += ONECARPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= ONECARPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}      
      else if(source == twoCarBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += TWOCARPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= TWOCARPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
      else if(source == threeCarBox)
{
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      totalPrice += THREECARPRICE;
      else if(select == ItemEvent.DESELECTED)
      totalPrice -= THREECARPRICE;
      output = "" + totalPrice;
      totPrice.setText(output);
}
}
}


zzynxSr. Software engineerCommented:
>> However when I click the boxes the amounts dont  show in the text field they show  beside it.

Of course:

      JTextField totalPrice = new JTextField(10);
      add(totalPrice);  // You add the TextField
      add(totPrice); // Besides you add the JLabel

and in your itemStateChanged() method I see:

       totPrice.setText(...);


So, you don't set the TextField's text, but the JLabel text

And as I said earlier: you should better implement ActionListener instead of ItemListener.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lovemycomputersAuthor Commented:
Thanks I figured it out with the ItemListener. I appreciate all your help Z. Thanks for putting up with me!
zzynxSr. Software engineerCommented:
You're welcome.
If you consider this question as completely answered, then please consider closing it by assiging points to the comments that helped you.
zzynxSr. Software engineerCommented:
Oh, you already did.
Thanx 4 axxepting.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.