Results 1 to 13 of 13

Thread: ButtonGroups

  1. #1
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default ButtonGroups

    I'm trying to create a application for an online class. They don't offer much help. It deals with buttongroups. The text book only has half a page talking about it but no code examples.

    I'm not sure how to do the public void actionPerformed with buttongroups or if the rest of the code is completely right.

    Please feel free to insult me if needed.


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent.*;
    import java.awt.event.ActionListener.*;
    
    public class JMyNewHome3 extends JFrame implements ActionListener
    {
    	int house1 = 100000;
    	int house2 = 120000;
    	int house3 = 180000;
    	int house4 = 250000;
    	int bedroom2 = 10500;
    	int bedroom3 = 21000;
    	int bedrrom4 = 31500;
    	int car0 = 0;
    	int car1 = 7775;
    	int car2 = 15500;
    	int car3 = 23325;
    	int car4 = 31100;
    	
    	ButtonGroup group1 = new ButtonGroup() ;
    	JCheckBox house1 = new JCheckBox("Aspen", false);
    	JCheckBox house2 = new JCheckBox("Brittany", false) ;
    	JCheckBox house3 = new JCheckBox("Colonial", false) ;
    	JCheckBox house4 = new JCheckBox("Dartmoor", false) ;
    		
    	ButtonGroup group2 = new ButtonGroup() ;
    	JCheckBox bedroom2 = new JCheckBox("2 Bedroom", false) ;
    	JCheckBox bedroom3 = new JCheckBox("3 Bedroom", false) ;
    	JCheckBox bedroom4 = new JCheckBox("4 Bedroom", false) ;
    	
    	ButtonGroup group3 = new ButtonGroup() ;
    	JCheckBox car0 = new JCheckBox("No Garage", false) ;
    	JCheckBox car1 = new JCheckBox("1 Car", false) ;
    	JCheckBox car2 = new JCheckBox("2 Car", false) ;
    	JCheckBox car3 = new JCheckBox("3 Car", false) ;
    	
    public JMyNewHome3()
    	{
    	super("My New Home");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setLayout(new FlowLayout());
    	add(house1) ;
    	add(house2) ;
    	add(house3) ;
    	add(house4) ;
    	add(bedroom2) ; 
    	add(bedroom3) ;
    	add(bedroom4) ;
    	add(car0) ;
    	add(car1) ;
    	add(car2) ;
    	add(car3) ;
    	house1.addActionListener(this);
    	house2.addActionListener(this);
    	house3.addActionListener(this);
    	house4.addActionListener(this);
    	bedroom2.addActionListener(this);
    	bedroom3.addActionListener(this);
    	bedroom4.addActionListener(this);
    	car0.addActionListener(this);
    	car1.addActionListener(this);
    	car2.addActionListener(this);
    	car3.addActionListener(this);
    	}
    public void actionPerformed(ActionEvent e)
    {
    	Object source = event.getSource();
    	int select = event.getStateChange();
    		if	
    }
    public static void main(String[] args)
    	{
    	JMyNewHome3 aFrame = new JMyNewHome3();
    	final int WIDTH = 300;
    	final int HEIGHT = 250;
    	aFrame.setSize(WIDTH, HEIGHT);
    	aFrame.setVisible(true);
    	}
    }
    Last edited by cheesehead11; 02-12-2011 at 08:19 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cheesehead11 View Post
    I'm trying to create a application for an online class. They don't offer much help. It deals with buttongroups. The text book only has half a page talking about it but no code examples.

    I'm not sure how to do the public void actionPerformed with buttongroups or if the rest of the code is completely right.

    Please feel free to insult me if needed.
    Insult number 1: What is the goal of the code? You need to tell us what you're trying to do and how it's not working for us to be able to fully help.

    Insult number 2: Please use code tags when posting code so we can better be able to read it. My signature below will tell you how.

    Also, the standard usage that I'm familiar with is to use JRadioButtons in ButtonGroups when you want a group of selectable buttons but only one should be selected, and to use JCheckBoxes without a ButtonGroup if you want a group of selectable buttons when one, many or none may be selected. Also, when using these selectable buttons, I often don't give them ActionListeners but rather add a single ActionListener to a single submit or accept JButton, so that when this button is pressed, the JRadioButtons (or if you want to be fancy, the ButtonGroups) are checked to see which have been selected.
    Last edited by Fubarable; 02-12-2011 at 07:28 PM.

  3. #3
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    the goal is to allow a person to order a house by choosing house models, bedrooms and garages. The application adds up the choices from the different groups.
    Last edited by cheesehead11; 02-12-2011 at 07:25 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cheesehead11 View Post
    the goal is to allow a person to order a house by choosing house models, bedrooms and garages. The application adds up the choices from the different groups.
    OK, thanks for the information. Now please edit your original post to add code tags. Also, does your application has a submit or accept or done button as per my previous reply? If not, perhaps you want to add one.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, thanks for the code tags!

    Now, my suggestion is to add a submit or accept JButton, and add the ActionListener to this only. Then in your actionPerformed method you can either go through each JRadioButton (for that's what I recommend that you should use) and check its state. Alternatively, a more compact bit of coding would use each ButtonGroup object and call the getSelection() method on it. This will return either the ButtonModel of the selected JRadioButton or null if no radio button has been selected. Once you have the ButtonModel object, you can get its actionCommand property via the getActionCommand() method. Make sure that you set the actionCommand though on each JRadioButton first as you're creating it via the setActionCommand.

    Please let me know if any of this is confusing.

  6. #6
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    This application doesn't require a submit or accept JButton.

    is this correct for the set.ActionCommand()?

    Java Code:
    	ButtonGroup group1 = new ButtonGroup() ;
    	JCheckBox house1 = new JCheckBox("Aspen", false);
    	house1.setActionCommand();
    	JCheckBox house2 = new JCheckBox("Brittany", false) ;
    	house2.setActionCommand();
    	JCheckBox house3 = new JCheckBox("Colonial", false) ;
    	house3.setActionCommand();
    	JCheckBox house4 = new JCheckBox("Dartmoor", false) ;
    	house4.setActionCommand();

    for the actionPerformed method this is as far as I can get. I have been searching around my book and internet and im lost.

    Java Code:
    public void actionPerformed(ActionEvent e)
    {
    	Object source = event.getSource();
    	int select = event.getStateChange();
    		if(source == group1)
    			if(select == get.ActionCommand.SELECTED)
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cheesehead11 View Post
    This application doesn't require a submit or accept JButton.
    Please show the actual assignment specification.


    is this correct for the set.ActionCommand()?

    Java Code:
    	ButtonGroup group1 = new ButtonGroup() ;
    	JCheckBox house1 = new JCheckBox("Aspen", false);
    	house1.setActionCommand();
    	JCheckBox house2 = new JCheckBox("Brittany", false) ;
    	house2.setActionCommand();
    	JCheckBox house3 = new JCheckBox("Colonial", false) ;
    	house3.setActionCommand();
    	JCheckBox house4 = new JCheckBox("Dartmoor", false) ;
    	house4.setActionCommand();
    No -- does the code compile? You need to pass a String into the setActionCommand method (and the compiler is likely telling you this already, no?), and it is often the same String that you use to set your button's text.


    for the actionPerformed method this is as far as I can get. I have been searching around my book and internet and im lost.

    Java Code:
    public void actionPerformed(ActionEvent e)
    {
    	Object source = event.getSource();
    	int select = event.getStateChange();
    		if(source == group1)
    			if(select == get.ActionCommand.SELECTED)
    }
    Again, does this even compile?

  8. #8
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    after a long night sleep and more research, I was able to do this:

    I am not sure how to do the setActionCommand in the buttonGroup.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent.*;
    import java.awt.event.ActionListener.*;
    
    public class JMyNewHome3 extends JFrame implements ActionListener
    {
    	int housePrice = 0;
    	int bedroomPrice = 0;
    	int carPrice = 0;
    	
    	ButtonGroup group1 = new ButtonGroup() ;
    	JCheckBox house1 = new JCheckBox("Aspen", false);
    	house1.setActionCommand("house 10000");
    	JCheckBox house2 = new JCheckBox("Brittany", false) ;
    	house2.setActionCommand("house" 120000);
    	JCheckBox house3 = new JCheckBox("Colonial", false) ;
    	house3.setActionCommand("house 180000");
    	JCheckBox house4 = new JCheckBox("Dartmoor", false) ;
    	house4.setActionCommand("house 250000");
    		
    	ButtonGroup group2 = new ButtonGroup() ;
    	JCheckBox bedroom2 = new JCheckBox("2 Bedroom", false) ;
    	bedroom2.setActionCommand("bedroom 10500");
    	JCheckBox bedroom3 = new JCheckBox("3 Bedroom", false) ;
    	bedroom3.setActionCommand("bedroom 21000");
    	JCheckBox bedroom4 = new JCheckBox("4 Bedroom", false) ;
    	bedroom4.setActionCommand("bedroom 31500");
    	
    	ButtonGroup group3 = new ButtonGroup() ;
    	JCheckBox car0 = new JCheckBox("No Garage", false) ;
    	car0.setActionCommand("car 7775");
    	JCheckBox car1 = new JCheckBox("1 Car", false) ;
    	car1.setActionCommand("car 15500");
    	JCheckBox car2 = new JCheckBox("2 Car", false) ;
    	car2.setActionCommand("car 23325");
    	JCheckBox car3 = new JCheckBox("3 Car", false) ;
    	car3.setActionCommand("car 31100");
    	
    public JMyNewHome3()
    	{
    	super("My New Home");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setLayout(new FlowLayout());
    	add(house1) ;
    	add(house2) ;
    	add(house3) ;
    	add(house4) ;
    	add(bedroom2) ;
    	add(bedroom3) ;
    	add(bedroom4) ;
    	add(car0) ;
    	add(car1) ;
    	add(car2) ;
    	add(car3) ;
    	house1.addActionListener(this);
    	house2.addActionListener(this);
    	house3.addActionListener(this);
    	house4.addActionListener(this);
    	bedroom2.addActionListener(this);
    	bedroom3.addActionListener(this);
    	bedroom4.addActionListener(this);
    	car0.addActionListener(this);
    	car1.addActionListener(this);
    	car2.addActionListener(this);
    	car3.addActionListener(this);
    	}
    public void actionPerformed(ActionEvent e)
    {
    	JLabel priceLabel = new JLabel();
    	String action = e.getActionCommand();
    		if (action.contains("house"))
    		{
    			try
    			{
    			housePrice = Integer.parseInt(action.string);
    			}
    			catch (Exception e)
    				{
    				e.printStackTrace();
    				}
    		}
    
    	
    		if (action.contains("house"))
    		{
    			try
    			{
    			bedroomPrice = Integer.parseInt(action.string);
    			}
    			catch (Exception e)
    				{
    				e.printStackTrace();
    				}
    		}
    
    		if (action.contains("house"))
    		{
    			try
    			{
    			carPrice = Integer.parseInt(action.string);
    			}
    			catch (Exception e)
    				{
    				e.printStackTrace();
    				}
    		}
    	priceLabel.setText("This house will cost $" + (housePrice + bedroomPrice + carPrice));
    }
    
    public static void main(String[] args)
    	{
    	JMyNewHome3 aFrame = new JMyNewHome3();
    	aFrame.setSize(300, 250);
    	aFrame.setVisible(true);
    	}
    }
    Last edited by cheesehead11; 02-13-2011 at 08:14 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    In this project, you'll create an application that will allow a new customer to order a house. You'll allow the customer to choose among four models (Aspen, Brittany, Colonial, and Dartmoor) by creating separate ButtonGroups. You'll also allow to customer to choose 2-4 bedrooms and finally, you'll allow the customer to choose a garage holding up to three cars. For each addition, include the following:

    Aspen = 100000
    Brittany = 120000
    Colonial = 180000
    Dartmoor = 250000
    Each additional bedroom = 10500
    each car (0-3) = 7775

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for posting that. I don't see anything that says you cannot use an accept or enter button to accept all of the user's selection. I also don't see anything that states that you must add actionlisteners to each button. Finally, there's nothing that states that you must use JCheckBoxes. My recommendations are thus unchanged. You are free to use them if you wish, but if you decide not to, then I won't be making any more recommendations.

    Best of luck.

  12. #12
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    when i try to compile, I get these errors for the actionCommand.

    JMyNewHome3.java:15: <identifier> expected
    house1.setActionCommand("house 10000");
    ^
    JMyNewHome3.java:15: illegal start of type
    house1.setActionCommand("house 10000");

  13. #13
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    I have been working on a new code. It compiles and runs, the only issue is doing the itemStateChanged. I'm not sure on how to write it for individual buttongroups.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import java.awt.event.ItemListener.*;
    import javax.swing.ButtonGroup.*;
    import java.lang.Object.*;
    
    public class JMyNewHome3 extends JFrame implements ItemListener
    {
    	JLabel totPrice = new JLabel();
    	final int home1 = 100000;
    	final int home2 = 120000;
    	final int home3 = 180000;
    	final int home4 = 250000;
    	final int bed2 = 10500;
    	final int bed3 = 21000;
    	final int bed4 = 31500;
    	final int gar0 = 0;
    	final int gar1 = 7775;
    	final int gar2 = 15500;
    	final int gar3 = 23325;
    	int housePrice = 0;
    	int bedroomPrice = 0;
    	int carPrice = 0;
    	
    	ButtonGroup group1 = new ButtonGroup();
    	JRadioButton house1 = new JRadioButton("Aspen", false);
    	JRadioButton house2 = new JRadioButton("Brittany", false);
    	JRadioButton house3 = new JRadioButton("Colonial", false);
    	JRadioButton house4 = new JRadioButton("Dartmoor", false);
    	
    		
    	ButtonGroup group2 = new ButtonGroup();
    	JRadioButton bedroom2 = new JRadioButton("2 Bedroom", false);
    	JRadioButton bedroom3 = new JRadioButton("3 Bedroom", false);
    	JRadioButton bedroom4 = new JRadioButton("4 Bedroom", false);
    	
    	
    	ButtonGroup group3 = new ButtonGroup();
    	JRadioButton car0 = new JRadioButton("No Garage", false);
    	JRadioButton car1 = new JRadioButton("1 Car", false);
    	JRadioButton car2 = new JRadioButton("2 Car", false);
    	JRadioButton car3 = new JRadioButton("3 Car", false);
    	
    	
    public JMyNewHome3()
    	{
    	super("My New Home");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setLayout(new FlowLayout());
    	add(house1);
    	add(house2);
    	add(house3);
    	add(house4);
    	add(bedroom2);
    	add(bedroom3);
    	add(bedroom4);
    	add(car0);
    	add(car1);
    	add(car2);
    	add(car3);
    	add(totPrice);
    	group1.add(house1);
    	group1.add(house2);
    	group1.add(house3);
    	group1.add(house4);
    	group2.add(bedroom2);
    	group2.add(bedroom3);
    	group2.add(bedroom4);
    	group3.add(car0);
    	group3.add(car1);
    	group3.add(car2);
    	group3.add(car3);
    	house1.addItemListener(this);
    	house2.addItemListener(this);
    	house3.addItemListener(this);
    	house4.addItemListener(this);
    	bedroom2.addItemListener(this);
    	bedroom3.addItemListener(this);
    	bedroom4.addItemListener(this);
    	car0.addItemListener(this);
    	car1.addItemListener(this);
    	car2.addItemListener(this);
    	car3.addItemListener(this);
    	}
    public void itemStateChanged(ItemEvent event)
    {
    	Object source = event.getSource();
    	int select = event.getStateChange();	
    		
    		if(source == house1)
    			housePrice = home1;
    		else 
    			housePrice = housePrice;
    		
    		if(source == house2)
    			housePrice = home2;
    		else 
    			housePrice = housePrice;
    		
    		if(source == house3)
    			housePrice = home3;
    		else 
    			housePrice = housePrice;
    		
    		if(source == house4)
    			housePrice = home4;
    		else 
    			housePrice = housePrice;
    			
    	
    		if(source == bedroom2)
    			bedroomPrice = bed2;
    		else 
    			housePrice = bedroomPrice;
    		
    		if(source == bedroom3)
    			bedroomPrice = bed3;
    		else 
    			housePrice = bedroomPrice;
    		
    		if(source == bedroom4)
    			bedroomPrice = bed4;
    		else 
    			bedroomPrice = bedroomPrice;
    
    		if(source == car0)
    			carPrice = gar0;
    		else 
    			carPrice = carPrice;
    		
    		if(source == car1)
    			carPrice = gar1;
    		else 
    			carPrice = carPrice;
    		
    		if(source == car2)
    			carPrice = home3;
    		else 
    			carPrice = carPrice;
    		
    		if(source == car3)
    			carPrice = gar3;
    		else 
    			carPrice = carPrice;
    		
    	totPrice.setText("$" + (housePrice + bedroomPrice + carPrice));
    
    }
    public static void main(String[] args)
    	{
    	JMyNewHome3 aFrame = new JMyNewHome3();
    	aFrame.setSize(300, 250);
    	aFrame.setVisible(true);
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •