File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Using buttons to select options

Wesley David

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.
Home1, $65,000
Home2, $75,000
Home3, $95,000
Home4, $115,000
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

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

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");
JPanel pane = new JPanel();
ButtonGroup homeGroup = new ButtonGroup();

totPrice.setText("$" + newHome1);

public static void main(String[] arguments)
JFrame aFrame = new NewHome();

public void itemStateChanged(ItemEvent event)
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"

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

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

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.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Using buttons to select options
It's not a secret anymore!