public class ButtonGroup

  1. Object
  2. ButtonGroup

ImplementsActionSource<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();

Constructors

public ButtonGroup()Creates a new instance of ButtonsGroup
public ButtonGroup(RadioButton... rb)Adds all the radio buttons to the group

Methods

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

Inherited methods

Constructor details

ButtonGroup

public ButtonGroup()
Creates a new instance of ButtonsGroup

ButtonGroup

public ButtonGroup(RadioButton... rb)
Adds all the radio buttons to the group

Method details

addAll

public void addAll(RadioButton... rb)
Adds the RadioButtons to the group

Parameters

rb RadioButton...
a RadioButtons to add

add

public void add(RadioButton rb)
Adds a RadioButton to the group

Parameters

rb RadioButton
a RadioButton to add

remove

public void remove(RadioButton rb)
removes a RadioButton from the group

Parameters

rb RadioButton
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 RadioButton
the radio button to set as selected

setSelected

public void setSelected(int index)
Sets the selected Radio button by index

Parameters

index int
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 int
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 ActionListener<ActionEvent>
the listener

removeActionListener

public void removeActionListener(ActionListener<ActionEvent> al)
Removes an action listener from all the buttons in the group

Parameters

al ActionListener<ActionEvent>
the listener