This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/6/19

trying to add checkbox to a button group but keeping an error

divinity divinity

2017/6/19

#
hi danpost/super_hippo I am doing this pizza program that uses a gui. I have been trying to add the checkboxes and labels to the group but keep getting and error. I think something is blocking it or i am missing somethings. can u tell me what is missing or what the compiler is hitting that is causing the error. here is the codes.
   ButtonGroup sizeGroup;
   
    
       

        pack();
    }// </editor-fold>                        
        //pizza size option
   
  //initalizing the radio button
    JRadioButton small = new JRadioButton("Small");
    JRadioButton medium = new JRadioButton("Medium");
    JRadioButton large = new JRadioButton("Large");
    JRadioButton family = new JRadioButton("Family");
    
       //initializing the label
      JCheckBox tomato = new JCheckBox("Tomato", false);
      JCheckBox greenpepper = new JCheckBox("Green Pepper", false);
      JCheckBox olives = new JCheckBox("Black Olives", false);
      JCheckBox mushrooms = new JCheckBox("Mushrooms", false);
      JCheckBox cheese = new JCheckBox("Extra Cheese", false);
      JCheckBox pepperoni = new JCheckBox("Pepperoni", false);
      JCheckBox tSausage = new JCheckBox("Sausage", false);
    
    JCheckBox memberscard = new JCheckBox("Members Card", false);
    
    //initializing the button group
    ButtonGroup bgroup = new ButtonGroup();
    bgroup.- this is where the error is coming from. it is telling me to create a class in button group/bgroup
Super_Hippo Super_Hippo

2017/6/19

#
In general, you can't have this line outside methods. If that is not the case: What is the - in line 29 meaning? and What is the exact error message?
divinity divinity

2017/6/19

#
i am positing over the codes. the error say to create a class in pizza mania package JRadioButton small = new JRadioButton("Small"); JRadioButton medium = new JRadioButton("Medium"); JRadioButton large = new JRadioButton("Large"); JRadioButton family = new JRadioButton("Family"); //initializing the label JCheckBox tomato = new JCheckBox("Tomato", false); JCheckBox greenpepper = new JCheckBox("Green Pepper", false); JCheckBox olives = new JCheckBox("Black Olives", false); JCheckBox mushrooms = new JCheckBox("Mushrooms", false); JCheckBox cheese = new JCheckBox("Extra Cheese", false); JCheckBox pepperoni = new JCheckBox("Pepperoni", false); JCheckBox tSausage = new JCheckBox("Sausage", false); JCheckBox memberscard = new JCheckBox("Members Card", false); //initializing the button group ButtonGroup bgroup = new ButtonGroup(); bgroup- the error say to create a class in "bgroup" in package pizzaMania
Super_Hippo Super_Hippo

2017/6/19

#
Despite the fact that the error message is slightly different, the code is the same, so I still don't know what you are doing in this line or if it is in a method or not.
divinity divinity

2017/6/19

#
was trying to add the checkboxes to the button group but keep getting the erro
Super_Hippo Super_Hippo

2017/6/19

#
Move line 29 in a method and then use the normal 'bgroup.methodName(parameter)'.
Yehuda Yehuda

2017/6/19

#
You can't call a void method outside of a method (as was mentioned three times previously), it has to be called inside a method (that's the way it always was). I anyway don't see why you would be using CheckBoxes if you want to include it in a ButtonGroup, you should just use RadioButtons. A ButtonGroup will make sure that there is only one AbstractButton in the group selected at once, check boxes are meant to have any number selected at a time.
danpost danpost

2017/6/20

#
Yehuda wrote...
You can't call a void method outside of a method (as was mentioned three times previously), it has to be called inside a method (that's the way it always was).
In general, that is the case. I would not quite go as far as saying that, however. Instead of "inside a method", I would rather say "inside a block of executable code." You can have class initialization code call 'static void' methods as well as create objects and call 'void' (non-static) methods on them. An example of this can be found in the AActor class of my Animation Support Class scenario. The entire block of initialization code could be placed in a separate 'static void' method and called from the main block.
divinity divinity

2017/6/20

#
thank you all
Yehuda Yehuda

2017/6/20

#
@danost OK I've never seen that before.
divinity divinity

2017/7/12

#
hi I have created the method and put the buttongroup in it. the error that I keep getting is this. create a local variable when i create this buttongroup: buttongroup pizzatype = new buttongroup() am trying to do something like this: ButtonGroup pizzaType = new ButtonGroup(): when am trying to add the crust, keep getting this errors: 1. cannot find symbol 2 create a local variable: thincrust, mediumcrust and crustLover: 3create a field and create a parameter. what can i do to fix this. this is the same pizza program as above. here is the codes.
  ButtonGroup pizzaSize = new ButtonGroup();
        pizzaSize.add(small);
        pizzaSize.add(medium);
        pizzaSize.add(large);
        pizzaSize.add(family);
        
        ButtonGroup pizzaType = new ButtonGroup();
        pizzaType.add(ThinCrust);
        pizzaType.add(MediumCrust);
        pizzaType.add(CrustLover);
        
   }
danpost danpost

2017/7/12

#
Have you created RadioButton objects for the types of crust?!!! (would it not be them that you add to the ButtonGroup object?)
divinity divinity

2017/7/13

#
how do you create a RadioButton. can use show me
Super_Hippo Super_Hippo

2017/7/14

#
I think danpost referred to the code you used above for the different sizes where you had:
JRadioButton small = new JRadioButton("Small");
//...
Do you have the same for the crusts?
JRadioButton ThinCrust = new JRadioButton("Thin Crust");
//...
You need to login to post a reply.