public class RadioButton

  1. Object
  2. Component
  3. Label
  4. Button
  5. RadioButton

ImplementsActionSource<ActionEvent>, Animation, Editable, IconHolder, ReleasableComponent, SelectableIconHolder, StyleListener, TextHolder

RadioButton is a Button that maintains a selection state exclusively within a specific ButtonGroup. Check out com.codename1.ui.CheckBox for a looser selection approach. Both components support a toggle button mode using the com.codename1.ui.Button#setToggle(boolean) API.

Form hi = new Form("Test RadioButton", 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 RadioButton(String text)Constructs a radio with the given text
public RadioButton()Creates an empty radio button
public RadioButton(Image icon)Constructs a radio with the given icon
public RadioButton(String text, Image icon)Constructs a radio with the given text and icon

Methods

public static RadioButton createToggle(String text, Image icon, ButtonGroup bg)Shorthand for creating the radio button, adding it to a group, setting the icon/text and making it into a toggle button
public static RadioButton createToggle(String text, ButtonGroup bg)Shorthand for creating the radio button, adding it to a group, setting the icon/text and making it into a toggle button
public static RadioButton createToggle(Image icon, ButtonGroup bg)Shorthand for creating the radio button, adding it to a group, setting the icon/text and making it into a toggle button
public static RadioButton createToggle(Command cmd, ButtonGroup bg)Shorthand for creating the radio button, adding it to a group, setting the command and making it into a toggle button
protected void initLaf(UIManager uim)This method initializes the Component defaults constants
protected void fireActionEvent(int x, int y)Allows subclasses to override action event behavior for pointer coordinates.
public String toString()Overriden to return a useful value for debugging purposes
public boolean isSelected()Returns true if the radio button is selected
public void setSelected(boolean selected)Selects the current radio button
public boolean isUnselectAllowed()Returns true if this RadioButton can be unselected
public void setUnselectAllowed(boolean unselectAllowed)Allows unselecting a selected RadioButton.
public void released(int x, int y)Invoked to change the state of the button to the released state
public void paint(Graphics g)This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API’s to let the PLAF perform the rendering.
protected Dimension calcPreferredSize()Calculates the preferred size based on component content.
public ButtonGroup getButtonGroup()Returns the parent button group
public String getGroup()This is a helper method to ease the usage of button groups
public void setGroup(String groupName)This is a helper method to ease the usage of button groups
public boolean isOppositeSide()Places the radio box on the opposite side at the far end
public void setOppositeSide(boolean oppositeSide)Places the radio box on the opposite side at the far end
public String[] getBindablePropertyNames()Returns the names of the properties within this component that can be bound for persistence, the order of these names mean that the first one will be the first bound
public Class[] getBindablePropertyTypes()Returns the types of the properties that are bindable within this component
public void bindProperty(String prop, BindTarget target)Deprecated Binds the given property name to the given bind target
public void unbindProperty(String prop, BindTarget target)Deprecated Removes a bind target from the given property name
public Object getBoundPropertyValue(String prop)Allows the binding code to extract the value of the property
public void setBoundPropertyValue(String prop, Object value)Sets the value of a bound property within this component, notice that this method MUST NOT fire the property change event when invoked to prevent recursion!
public void addChangeListener(ActionListener l)Adds a listener to be notified when the the checkbox’s selected value changes.
public void removeChangeListeners(ActionListener l)Removes a change change listener.

Inherited fields

Inherited methods

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getUIManager, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, clearClientProperties, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getScrollable, paintBackground, isScrollable, isScrollableX, isScrollableY, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, getBottomGap, getSideGap, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, setShouldCalcPreferredSize, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, stopScrollMomentum, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, drop, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, setPressedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, refreshTheme, refreshTheme, isDragActivated, getGridPosY, getGridPosX, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, setCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, deinitialize, initComponent, isInitialized, setInitialized, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, isEnabled, setEnabled, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, cancelRepaints, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip

Constructor details

RadioButton

public RadioButton(String text)
Constructs a radio with the given text

Parameters

text String
to display next to the button

RadioButton

public RadioButton()
Creates an empty radio button

RadioButton

public RadioButton(Image icon)
Constructs a radio with the given icon

Parameters

icon Image
icon to show next to the button

RadioButton

public RadioButton(String text, Image icon)
Constructs a radio with the given text and icon

Parameters

text String
to display next to the button
icon Image
icon to show next to the button

Method details

createToggle

public static RadioButton createToggle(String text, Image icon, ButtonGroup bg)
Shorthand for creating the radio button, adding it to a group, setting the icon/text and making it into a toggle button

Parameters

text String
the text for the button
icon Image
the icon for the button
bg ButtonGroup
the button group

Returns

a radio button

createToggle

public static RadioButton createToggle(String text, ButtonGroup bg)
Shorthand for creating the radio button, adding it to a group, setting the icon/text and making it into a toggle button

Parameters

text String
the text for the button
bg ButtonGroup
the button group

Returns

a radio button

createToggle

public static RadioButton createToggle(Image icon, ButtonGroup bg)
Shorthand for creating the radio button, adding it to a group, setting the icon/text and making it into a toggle button

Parameters

icon Image
the icon for the button
bg ButtonGroup
the button group

Returns

a radio button

createToggle

public static RadioButton createToggle(Command cmd, ButtonGroup bg)
Shorthand for creating the radio button, adding it to a group, setting the command and making it into a toggle button

Parameters

cmd Command
the command
bg ButtonGroup
the button group

Returns

a radio button

initLaf

protected void initLaf(UIManager uim)
This method initializes the Component defaults constants

fireActionEvent

protected void fireActionEvent(int x, int y)
Allows subclasses to override action event behavior for pointer coordinates.

Parameters

x int
the x position of the click if applicable (can be 0 or -1 otherwise)
y int
the y position of the click if applicable (can be 0 or -1 otherwise)

toString

public String toString()
Overriden to return a useful value for debugging purposes

Returns

a string representation of this component

isSelected

public boolean isSelected()
Returns true if the radio button is selected

Returns

true if the radio button is selected

setSelected

public void setSelected(boolean selected)
Selects the current radio button

Parameters

selected boolean
value for selection

isUnselectAllowed

public boolean isUnselectAllowed()
Returns true if this RadioButton can be unselected

Returns

true to allow deselection of radio buttons

setUnselectAllowed

public void setUnselectAllowed(boolean unselectAllowed)
Allows unselecting a selected RadioButton. This is useful for when implementing a ButtonGroup that allows no selection or a single selection.,

Parameters

unselectAllowed boolean
true to allow deselection of a radio button, false for the default behavior

released

public void released(int x, int y)
Invoked to change the state of the button to the released state

Parameters

x int
the x position if a touch event triggered this, -1 if this isn’t relevant
y int
the y position if a touch event triggered this, -1 if this isn’t relevant

paint

public void paint(Graphics g)
This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API’s to let the PLAF perform the rendering.

Parameters

g Graphics
the component graphics

calcPreferredSize

protected Dimension calcPreferredSize()
Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

Returns

the calculated preferred size based on component content

getButtonGroup

public ButtonGroup getButtonGroup()
Returns the parent button group

Returns

the parent button group

getGroup

public String getGroup()
This is a helper method to ease the usage of button groups

Returns

the name of the group

setGroup

public void setGroup(String groupName)
This is a helper method to ease the usage of button groups

Parameters

groupName String
a name for the goup

isOppositeSide

public boolean isOppositeSide()
Places the radio box on the opposite side at the far end

Returns

the oppositeSide

setOppositeSide

public void setOppositeSide(boolean oppositeSide)
Places the radio box on the opposite side at the far end

Parameters

oppositeSide boolean
the oppositeSide to set

getBindablePropertyNames

public String[] getBindablePropertyNames()
Returns the names of the properties within this component that can be bound for persistence, the order of these names mean that the first one will be the first bound

Returns

a string array of property names or null

getBindablePropertyTypes

public Class[] getBindablePropertyTypes()
Returns the types of the properties that are bindable within this component

Returns

the class for binding

bindProperty

public void bindProperty(String prop, BindTarget target)
Deprecated. uses the deprecated BindTarget interface
Binds the given property name to the given bind target

Parameters

prop String
the property name
target BindTarget
the target binder

unbindProperty

public void unbindProperty(String prop, BindTarget target)
Deprecated. uses the deprecated BindTarget interface
Removes a bind target from the given property name

Parameters

prop String
the property names
target BindTarget
the target binder

getBoundPropertyValue

public Object getBoundPropertyValue(String prop)
Allows the binding code to extract the value of the property

Parameters

prop String
the property

Returns

the value for the property

setBoundPropertyValue

public void setBoundPropertyValue(String prop, Object value)
Sets the value of a bound property within this component, notice that this method MUST NOT fire the property change event when invoked to prevent recursion!

Parameters

prop String
the property whose value should be set
value Object
the value

addChangeListener

public void addChangeListener(ActionListener l)
Adds a listener to be notified when the the checkbox’s selected value changes. The difference between a change listener and an action listener is that a change listener is fired whenever there is a change, but action events are only fired when the change is a result of the user clicking on the checkbox.

Parameters

l ActionListener
Listener to be notified when selected value changes.

See also

  • #removeChangeListener(com.codename1.ui.events.ActionListener)

removeChangeListeners

public void removeChangeListeners(ActionListener l)
Removes a change change listener.