How do I add radio buttons to a button group using NetBeans?
Once I add them, how do I get selected radio button from the button group?
This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same ButtonGroup object means that turning "on" one of those buttons turns off all other buttons in the group. A ButtonGroup can be used with any set of objects that inherit from AbstractButton .
A button group is a container control that holds push button controls. The button group defines the layout for the buttons it contains, including alignment within the container and the title for the group. Place this control within a section control or a table control.
ButtonGroup
from the palette and drop it on your GUI.
It will show up under Other Components in the Inspector panel.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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With