public class CheckBox

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

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

CheckBox is a button that can be selected or deselected and displays its state to the user. Check out com.codename1.ui.RadioButton for a more exclusive selection approach. Both components support a toggle button mode using the com.codename1.ui.Button#setToggle(boolean) API.

Form hi = new Form("Test CheckBox", 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 CheckBox(String text)Constructs a checkbox with the given text
public CheckBox()Constructs a checkbox with no text
public CheckBox(Image icon)Constructs a checkbox with the given icon
public CheckBox(String text, Image icon)Constructs a checkbox with the given text and icon

Methods

public static CheckBox createToggle(String text, Image icon)Shorthand for creating the check box setting the icon/text and making it into a toggle button
public static CheckBox createToggle(String text)Shorthand for creating the check box setting the icon/text and making it into a toggle button
public static CheckBox createToggle(Image icon)Shorthand for creating the check box setting the icon/text and making it into a toggle button
public boolean isSelected()Return true if the checkbox is selected
public void setSelected(boolean selected)Selects the current checkbox
public void released(int x, int y)Invoked to change the state of the button to the released state
protected void fireActionEvent(int x, int y)Allows subclasses to override action event behavior for pointer coordinates.
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.
protected String paramString()Returns a string representing the state of this component.
public void refreshTheme(boolean merge)Makes sure the component is up to date with the current theme, ONLY INVOKE THIS METHOD IF YOU CHANGED THE THEME!
public boolean isOppositeSide()Places the check box on the opposite side at the far end
public void setOppositeSide(boolean oppositeSide)Places the check 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, toString, 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

CheckBox

public CheckBox(String text)
Constructs a checkbox with the given text

Parameters

text String
to display next to the checkbox

CheckBox

public CheckBox()
Constructs a checkbox with no text

CheckBox

public CheckBox(Image icon)
Constructs a checkbox with the given icon

Parameters

icon Image
icon to display next to the checkbox

CheckBox

public CheckBox(String text, Image icon)
Constructs a checkbox with the given text and icon

Parameters

text String
to display next to the checkbox
icon Image
icon to display next to the text

Method details

createToggle

public static CheckBox createToggle(String text, Image icon)
Shorthand for creating the check box 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

Returns

a check box

createToggle

public static CheckBox createToggle(String text)
Shorthand for creating the check box setting the icon/text and making it into a toggle button

Parameters

text String
the text for the button

Returns

a check box

createToggle

public static CheckBox createToggle(Image icon)
Shorthand for creating the check box setting the icon/text and making it into a toggle button

Parameters

icon Image
the icon for the button

Returns

a check box

isSelected

public boolean isSelected()
Return true if the checkbox is selected

Returns

true if the checkbox is selected

setSelected

public void setSelected(boolean selected)
Selects the current checkbox

Parameters

selected boolean
value for selection

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

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)

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

paramString

protected String paramString()
Returns a string representing the state of this component. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Returns

a string representation of this component’s state

refreshTheme

public void refreshTheme(boolean merge)
Makes sure the component is up to date with the current theme, ONLY INVOKE THIS METHOD IF YOU CHANGED THE THEME!

Parameters

merge boolean
indicates if the current styles should be merged with the new styles

isOppositeSide

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

Returns

the oppositeSide

setOppositeSide

public void setOppositeSide(boolean oppositeSide)
Places the check 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.