# Class ButtonGroup

java.lang.Object
com.codename1.ui.ButtonGroup

All Implemented Interfaces:
  `ActionSource<ActionEvent>`

public class ButtonGroup extends Object implements ActionSource<ActionEvent>

This class is used to create a multiple-exclusion scope for `com.codename1.ui.RadioButton`. Creating a set of `com.codename1.ui.RadioButton` components with the same `ButtonGroup` object means that only one `com.codename1.ui.RadioButton` can be selected among those within the specific `ButtonGroup`.

    Form hi = new Form("Test ButtonGroup", BoxLayout.y());

    Image icon = FontImage.createMaterial(FontImage.MATERIAL_INFO, "CheckBox", 3.0f);

    CheckBox cb1 = new CheckBox("CheckBox No Icon");
    cb1.setSelected(true);
    CheckBox cb2 = new CheckBox("CheckBox With Icon", icon);
    CheckBox cb3 = new CheckBox("CheckBox Opposite True", icon);
    CheckBox cb4 = new CheckBox("CheckBox Opposite False", icon);
    cb3.setOppositeSide(true);
    cb4.setOppositeSide(false);
    RadioButton rb1 = new RadioButton("Radio 1");
    RadioButton rb2 = new RadioButton("Radio 2");
    RadioButton rb3 = new RadioButton("Radio 3", icon);
    new ButtonGroup(rb1, rb2, rb3);
    rb2.setSelected(true);
    hi.add(cb1).add(cb2).add(cb3).add(cb4).add(rb1).add(rb2).add(rb3);
    hi.show();

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ButtonGroup()`
  Creates a new instance of ButtonsGroup
  `ButtonGroup(RadioButton... rb)`
  Adds all the radio buttons to the group

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `add(RadioButton rb)`
  Adds a RadioButton to the group
  `void`
  `addActionListener(ActionListener<ActionEvent> al)`
  Adds an action listener to all the buttons in the group
  `void`
  `addAll(RadioButton... rb)`
  Adds the RadioButtons to the group
  `void`
  `clearSelection()`
  Clears the selection such that none of the buttons in the ButtonGroup are selected.
  `int`
  `getButtonCount()`
  Returns the number of buttons in the group.
  `RadioButton`
  `getRadioButton(int index)`
  Returns the radio button at the given group index
  `RadioButton`
  `getSelected()`
  Return the selected radio button within the group
  `int`
  `getSelectedIndex()`
  Return the index of the selected button within the group
  `boolean`
  `isSelected()`
  Returns whether a radio button in the group is selected.
  `void`
  `remove(RadioButton rb)`
  removes a RadioButton from the group
  `void`
  `removeActionListener(ActionListener<ActionEvent> al)`
  Removes an action listener from all the buttons in the group
  `void`
  `setSelected(int index)`
  Sets the selected Radio button by index
  `void`
  `setSelected(RadioButton rb)`
  Selects the given radio button

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ButtonGroup

    public ButtonGroup()
    Creates a new instance of ButtonsGroup

  - ### ButtonGroup

    public ButtonGroup(RadioButton... rb)

    Adds all the radio buttons to the group

    ###### Parameters

    - `rb`

- ## Method Details

  - ### addAll

    public void addAll(RadioButton... rb)

    Adds the RadioButtons to the group

    ###### Parameters

    - `rb`: a RadioButtons to add

  - ### add

    public void add(RadioButton rb)

    Adds a RadioButton to the group

    ###### Parameters

    - `rb`: a RadioButton to add

  - ### remove

    public void remove(RadioButton rb)

    removes a RadioButton from the group

    ###### Parameters

    - `rb`: a RadioButton to remove

  - ### clearSelection

    public void clearSelection()
    Clears the selection such that none of the buttons in the ButtonGroup are selected.

  - ### getButtonCount

    public int getButtonCount()

    Returns the number of buttons in the group.

    ###### Returns

    number of radio buttons in the group

  - ### isSelected

    public boolean isSelected()

    Returns whether a radio button in the group is selected.

    ###### Returns

    true if a selection was made in the radio button group

  - ### getSelectedIndex

    public int getSelectedIndex()

    Return the index of the selected button within the group

    ###### Returns

    the index of the selected button within the group

  - ### getSelected

    public RadioButton getSelected()

    Return the selected radio button within the group

    ###### Returns

    the selected radio button within the group

  - ### setSelected

    public void setSelected(RadioButton rb)

    Selects the given radio button

    ###### Parameters

    - `rb`: the radio button to set as selected

  - ### setSelected

    public void setSelected(int index)

    Sets the selected Radio button by index

    ###### Parameters

    - `index`: the index of the radio button to mark as selected

  - ### getRadioButton

    public RadioButton getRadioButton(int index)

    Returns the radio button at the given group index

    ###### Parameters

    - `index`: offset within the group starting with 0 and no larger than getButtonCount()

    ###### Returns

    the radio button instance

  - ### addActionListener

    public void addActionListener(ActionListener<ActionEvent> al)

    Adds an action listener to all the buttons in the group

    ###### Parameters

    - `al`: the listener
    Specified by:
      `addActionListener` in interface `ActionSource<ActionEvent>`

  - ### removeActionListener

    public void removeActionListener(ActionListener<ActionEvent> al)

    Removes an action listener from all the buttons in the group

    ###### Parameters

    - `al`: the listener
    Specified by:
      `removeActionListener` in interface `ActionSource<ActionEvent>`