aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Using buttons to select options Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using buttons to select options" Watch "Using buttons to select options" New topic
Author

Using buttons to select options

Wesley David
Greenhorn

Joined: Dec 17, 2003
Posts: 6
Can someone help me to layout the foundation for selecting options with buttons and totaling the outcome. Here is my scenario.
Write a swing application for a home builder to handle a customer�s order for new home construction. Use separate ButtonGroups to allow the customer to select one of four models, the number of bedrooms, and the size of garage.
Model:
Home1, $65,000
Home2, $75,000
Home3, $95,000
Home4, $115,000
Bedrooms:
Bedroom2
Bedroom3
Bedroom4
Garage:
Garage0, $0
Garage1, $15,550
Garage2, $23,325
Garage3, $32,000
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
build up action listeners for each button group and register each group to listener. if(action is an instance of button1)blah...
then do your calculations when you've gathered all the info.
[ January 29, 2004: Message edited by: Tony Fabeets ]
Wesley David
Greenhorn

Joined: Dec 17, 2003
Posts: 6
Would anyone happen to have code of somehting similar to this example?
Wesley David
Greenhorn

Joined: Dec 17, 2003
Posts: 6
I am sure this is simple, but I cannot figure out how to use the button group class for different choice selections. Example, if I was totaling values for a home and the different choices, I need the user to select different options (i.e. type of house, number of bedrooms, size of garage, etc.) Each choice within these different options should override the other. Below I have coded the first choice (the type of house), but I am unsure how to code the other option without overriding the house selections. Can I have multiple button group classes to do this? If so, I am unsure how to code it. Any help you can give me is appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewHome extends JFrame implements ItemListener
{
int newHome1 = 100000;
int newHome2 = 120000;
int newHome3 = 180000;
int newHome4 = 250000;

JCheckBox homeBox1 = new JCheckBox("Aspen $" + newHome1,true);
JCheckBox homeBox2 = new JCheckBox("Brittany $" + newHome2,false);
JCheckBox homeBox3 = new JCheckBox("Colonial $" + newHome3,false);
JCheckBox homeBox4 = new JCheckBox("Dartmoor $" + newHome4,false);

JLabel homeBuildersLabel = new JLabel("Home Builders Inc.");
JLabel homeOptionLabel = new JLabel("Select the desired model");
JLabel homeOptionLabel0 = new JLabel("");
JLabel ePrice = new JLabel("The price of your home is:");
JTextField totPrice = new JTextField(10);

public NewHome()
{
super("Home Price Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(homeBuildersLabel);
pane.add(homeOptionLabel);
pane.add(homeOptionLabel0);
pane.add(homeBox1);
pane.add(homeBox2);
pane.add(homeBox3);
pane.add(homeBox4);
ButtonGroup homeGroup = new ButtonGroup();
homeGroup.add(homeBox1);
homeGroup.add(homeBox2);
homeGroup.add(homeBox3);
homeGroup.add(homeBox4);
pane.add(ePrice);
pane.add(totPrice);

totPrice.setText("$" + newHome1);
homeBox1.addItemListener(this);
homeBox2.addItemListener(this);
homeBox3.addItemListener(this);
homeBox4.addItemListener(this);
setContentPane(pane);
}

public static void main(String[] arguments)
{
JFrame aFrame = new NewHome();
aFrame.setSize(250,500);
aFrame.setVisible(true);
}

public void itemStateChanged(ItemEvent event)
{
if(homeBox1.isSelected())
totPrice.setText("$" + newHome1);
else if(homeBox2.isSelected())
totPrice.setText("$" + newHome2);
else if(homeBox3.isSelected())
totPrice.setText("$" + newHome3);
else // if(homeBox4.isSelected())
totPrice.setText("$" + newHome4);
}
}
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Moved to the swing forum.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Yes, you can use separate ButtonGroup instances for each of the groupings. You currently have homeGroup to group all the types of homes together. Next you will create all the options for the number of bedrooms, and create another ButtonGroup (called bedroomGroup, probably) and then create all the options for garages and create a new ButtonGroup (garageGroup) to hold these options. All that a ButtonGroup really does is enforce that only one button (or check box, etc. ) that it contains can be selected at a time.

One problem you will run into with the way the code is currently written is in the itemStateChanged() method. Currently you are just setting the textfield with the value of the selected option directly. If you keep doing it this way, you are going to end up only getting the value of the last group instead of a total like you want. You're going to have to do something like total all the selected options and then put this value in the textfield.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using buttons to select options