Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,308 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,089 people online right now. Registration is fast and FREE... Join Now!




Swing Components

 

Swing Components, My Compiles aren't working ... Don't know where to start to fi

lprice292

21 May, 2009 - 05:33 PM
Post #1

New D.I.C Head
*

Joined: 7 Mar, 2009
Posts: 11

CODE

/*
Purpose: Create a program that allows user to select from to Jbutton groups
thie first allows the to pick from 4 housing models, the second allows them
to pick the number of car in a garage IF GARAGE IS ADDED THEN IT IS 7775 PER CAR. Display aprorpriate results in a Jtext.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMyNewHome extends JFrame implements ItemListener
{
      
    
    
     final  ASPEN = 100000;
     final BRITTANY = 120000;
     final  COLONIAL = 180000;
     final  DARTMOOR = 250000;
     int[] GARAGE_NUMBER = {0,1,2,3};
     int Garage = 7775;
    int totalPrice;
    // Checkbox declarations. Sets prices for the models.//
    
    JCheckBox aspenBox = new
        JCheckBox("Aspen $" + ASPEN, false);
    JCheckBox brittanyBox = new
        JCheckBox("Brittany $" + BRITTANY, true);
    JCheckBox colonialBox = new
        JCheckBox("Colonial $" + COLONIAL, false);
    JCheckBox dartmoorBox = new
        JCheckBox(" Dartmoor $" + DARTMOOR, false);
        
    // JLabel select garage functions.//
    
    JLabel garageLabel = new
        JLabel("Garage: Pick from here");
    String[] numGarage = {"None " + GARAGE_NUMBER[0],
        "1-Car " + GARAGE_NUMBER[1],
        "2-Car " + GARAGE_NUMBER[2],
        "3-Car " + GARAGE_NUMBER[3]};
    JComboBox garageBox = new JComboBox(numGarage);
    Container con;
    JPanel panel = new JPanel();
    
    JLabel priceLabel = new JLabel();
    JTextField totPrice = new JTextField(10);
    JTextField message = new JTextField(30);
    JLabel optionLabel = new JLabel
        ("Pricing based on current Market.");
public JMyNewHome()
{
    super("Homes Pricing");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    con = getContentPanel();
    con.add(panel);
    ButtonGroup houseGroup = new ButtonGroup();
    houseGroup.add(ASPEN);
    houseGroup.add(BRITTANY);
    houseGroup.add(COLONIAL);
    houseGroup.add(DARTMOOR);
    panel.add(ASPEN);
    panel.add(BRITTANY);
    panel.add(COLONIAL);
    panel.add(DARTMOOR);
    panel.add(garageLabel);
    panel.add(priceLabel);
    panel.add(totPrice);
    panel.add(message);
    totPrice.setText("$" + totalPrice);
    aspenBox.additemlistener(this);
    brittanyBox.additemlistener(this);
    colonialBox.additemlistener(this);
    dartmoorBox.additemlistener(this);
    garageBox.additemlistener(this);
    }
    
public static void main(String[] args)
    {
        final int WIDTH = 400;
        final int HEIGHT = 250;
        JMyNewHome aFrame = new
            JMyNewHome();
        aFrame.setSize(WIDTH, HEIGHT);
        aFrame.setVisible(true);
        }
        public void itemStateChanged(ItemEvent event)
        {
            Object source = event.getsource();
            int select = event.getStateChanged();
            if(source == aspenBox)
                if(select == ItemEvent.SELECTED)
                    message.setText("Action: Aspen selected");
                else
                    message.setText("Action: Apsen deslected");
                else if(source == garageBox)
                {
                    int numGarage = garageBox.getselectedIndex();
                    
        }
    
    }
    
}


User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Swing Components

21 May, 2009 - 06:25 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,241



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Quite a few things to fix here. Remember that Java is CASE SENSITIVE! Many of your errors is because you had a lowercase "s" when you should have had an uppercase "S". You also had an extra "d" when there is no d in the method name.

You also forgot data types on your variables and you don't add the variables to the GUI, you add the checkbox controls to the GUI.

java

/*
Purpose: Create a program that allows user to select from to Jbutton groups
thie first allows the to pick from 4 housing models, the second allows them
to pick the number of car in a garage IF GARAGE IS ADDED THEN IT IS 7775 PER CAR. Display aprorpriate results in a Jtext.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMyNewHome extends JFrame implements ItemListener
{

// Make sure you specify data type for your variables.
final int ASPEN = 100000;
final int BRITTANY = 120000;
final int COLONIAL = 180000;
final int DARTMOOR = 250000;

int[] GARAGE_NUMBER = {0,1,2,3};
int Garage = 7775;
int totalPrice;

// Checkbox declarations. Sets prices for the models.//
JCheckBox aspenBox = new JCheckBox("Aspen $" + ASPEN, false);
JCheckBox brittanyBox = new JCheckBox("Brittany $" + BRITTANY, true);
JCheckBox colonialBox = new JCheckBox("Colonial $" + COLONIAL, false);
JCheckBox dartmoorBox = new JCheckBox(" Dartmoor $" + DARTMOOR, false);

// JLabel select garage functions.//
JLabel garageLabel = new JLabel("Garage: Pick from here");
String[] numGarage = {"None " + GARAGE_NUMBER[0],
"1-Car " + GARAGE_NUMBER[1],
"2-Car " + GARAGE_NUMBER[2],
"3-Car " + GARAGE_NUMBER[3]};

JComboBox garageBox = new JComboBox(numGarage);
Container con;
JPanel panel = new JPanel();

JLabel priceLabel = new JLabel();
JTextField totPrice = new JTextField(10);
JTextField message = new JTextField(30);
JLabel optionLabel = new JLabel("Pricing based on current Market.");

public JMyNewHome()
{
super("Homes Pricing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ButtonGroup houseGroup = new ButtonGroup();

// Add checkboxes to our button group
houseGroup.add(aspenBox);
houseGroup.add(brittanyBox);
houseGroup.add(colonialBox);
houseGroup.add(dartmoorBox);

// Add buttons, not the values, to the panel.
panel.add(aspenBox);
panel.add(brittanyBox);
panel.add(colonialBox);
panel.add(dartmoorBox);
panel.add(garageLabel);

panel.add(priceLabel);
panel.add(totPrice);
panel.add(message);

totPrice.setText("$" + totalPrice);

// Remember, Java is case sensitive! It is addItemListener, not additemlistener
aspenBox.addItemListener(this);
brittanyBox.addItemListener(this);
colonialBox.addItemListener(this);
dartmoorBox.addItemListener(this);
garageBox.addItemListener(this);

// No need for getContentPane, just add it to the frame.
add(panel);
}

public static void main(String[] args)
{
final int WIDTH = 400;
final int HEIGHT = 250;
JMyNewHome aFrame = new JMyNewHome();
aFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
}

public void itemStateChanged(ItemEvent event)
{
// getSource has a capital "S"
Object source = event.getSource();

// getStateChange, not getStateChanged
int select = event.getStateChange();
if(source == aspenBox)
if(select == ItemEvent.SELECTED)
message.setText("Action: Aspen selected");
else
message.setText("Action: Apsen deslected");
else if(source == garageBox)
{
// Capital "S" in getSelectedIndex
int numGarage = garageBox.getSelectedIndex();

}
}

}


Read through the extra comments I put in. They will explain to you were your errors are and how they were fixed. And in the future, explain the problem rather than just throwing your code out there.

We are brilliant people, but we are not here to be your compilers.

Thanks! Enjoy!

"At DIC we be aspenBox and garageBox adding code ninjas... maybe we should become a garageBox band too!" decap.gif

This post has been edited by Martyr2: 21 May, 2009 - 06:26 PM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 01:49PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month