views:

262

answers:

4

How to add contents of button group in to netbeans using radio buttons?

how to get selected item of radiobuttons in buttongroup?

+1  A: 

I highly recommend reading this excellent tutorial. Here's an excerpt of code from the article that satisfies your question on how to create and add buttons to a ButtonGroup:

JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setSelected(true);

JRadioButton catButton = new JRadioButton(catString);

   //Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

As far as getting which item is selected, you basically need to iterate through the items in the group calling isSelected.

Mark E
+1  A: 

How to Use Buttons, Check Boxes, and Radio Buttons

ButtonGroup group = new ButtonGroup();
group.add(new JRadioButton("one"));
group.add(new JRadioButton("two"));
//TO FIND SELECTED
//use a loop on group.getElements();
//and check isSelected() and add them
//to some sort of data structure
twodayslate
+1  A: 

Darryl's Select Button Group will solve your problem.

camickr
+1  A: 
  1. Drag a ButtonGroup from the palette and drop it on your GUI. It will show up under Other Components in the Inspector panel.
  2. Right-click on it and Change variable name to something meaningful.
  3. Now select a radio button in your GUI.
  4. In the Properties panel look for the buttonGroup property.
  5. Click the combo box next to it and select your button group.
Devon_C_Miller