public class Button
ImplementsActionSource<ActionEvent>, Animation, Editable, IconHolder, ReleasableComponent, SelectableIconHolder, StyleListener, TextHolder
Known subtypesFloatingActionButton, ScaleImageButton, ShareButton, LikeButton, CheckBox, RadioButton, Picker
Button is the base class for several UI widgets allowing clickability.
It has 3 states: rollover, pressed and the default state. Button
can also have an com.codename1.ui.events.ActionListener that react when the
Button is clicked or handle actions via a
com.codename1.ui.Command.
Button has the “Button” UIID by default.
Here is trivial usage of the Button API:
Form hi = new Form("Button");
Button b = new Button("My Button");
hi.add(b);
b.addActionListener((e) -> Log.p("Clicked"));
This code shows a common use case of making a button look like a hyperlink
Form hi = new Form("Button");
Button b = new Button("Link Button");
b.getAllStyles().setBorder(Border.createEmpty());
b.getAllStyles().setTextDecoration(Style.TEXT_DECORATION_UNDERLINE);
hi.add(b);
b.addActionListener((e) -> Log.p("Clicked"));
Fields
public static final int STATE_ROLLOVER = 0 | Indicates the rollover state of a button which is equivalent to focused for most uses |
public static final int STATE_PRESSED = 1 | Indicates the pressed state of a button |
public static final int STATE_DEFAULT = 2 | Indicates the default state of a button which is neither pressed nor focused |
Constructors
Methods
public static boolean isCapsTextDefault() | Indicates whether text on the button should be drawn capitalized by default to match the Android design. |
public static void setCapsTextDefault(boolean aCapsTextDefault) | Indicates whether text on the button should be drawn capitalized by default to match the Android design This value can be set by the capsButtonTextBool theme constant |
public static boolean isButtonRippleEffectDefault() | Default value for the button ripple effect, this can be set with the theme constant buttonRippleBool |
public static void setButtonRippleEffectDefault(boolean aButtonRippleEffectDefault) | Default value for the button ripple effect, this can be set with the theme constant buttonRippleBool |
public void bindStateTo(Button button) | Bind the state of this button to another button’s state. |
public void unbindStateFrom(Button button) | Unbinds the state of this button from another button. |
protected void resetFocusable() | Restores the state of the focusable flag to its default state |
public int getState() | Returns the button state |
public void setReleased() | Set the button in released and unfocused state |
public Image getPressedIcon() | Indicates the icon that is displayed on the button when the button is in pressed state |
public void setPressedIcon(Image pressedIcon) | Indicates the icon that is displayed on the button when the button is in pressed state |
public Image getRolloverPressedIcon() | Indicates the icon that is displayed on the button when the button is in pressed state and is selected. |
public void setRolloverPressedIcon(Image rolloverPressedIcon) | Indicates the icon that is displayed on the button when the button is in pressed state and is selected. |
public Image getDisabledIcon() | Indicates the icon that is displayed on the button when the button is in the disabled state |
public void setDisabledIcon(Image disabledIcon) | Indicates the icon that is displayed on the button when the button is in the disabled state |
public Image getRolloverIcon() | Indicates the icon that is displayed on the button when the button is in rolled over state |
public void setRolloverIcon(Image rolloverIcon) | Indicates the icon that is displayed on the button when the button is in rolled over state |
public void addStateChangeListener(ActionListener<ComponentStateChangeEvent> l) | Adds a listener to be notified when the button state changes. |
public void removeStateChangeListener(ActionListener<ComponentStateChangeEvent> l) | Removes state change listener. |
public void addActionListener(ActionListener<ActionEvent> l) | Adds a listener to the button which will cause an event to dispatch on click |
public void removeActionListener(ActionListener<ActionEvent> l) | Removes the given action listener from the button |
public Vector<ActionListener<ActionEvent>> getActionListeners() | Deprecated Returns a vector containing the action listeners for this button |
public Collection<ActionListener<ActionEvent>> getListeners() | Returns a collection containing the action listeners for this button |
public Image getIconFromState() | Returns the icon for the button based on its current state |
protected void fireActionEvent(int x, int y) | Allows subclasses to override action event behavior for pointer coordinates. |
public void pressed() | Invoked to change the state of the button to the pressed state |
public void released() | Invoked to change the state of the button to the released state |
public void released(int x, int y) | Invoked to change the state of the button to the released state |
public void keyPressed(int keyCode) | If this Component is focused, the key pressed event will call this method |
public void keyReleased(int keyCode) | If this Component is focused, the key released event will call this method |
public void keyRepeated(int keyCode) | If this Component is focused, the key repeat event will call this method. |
protected void fireClicked() | When working in 3 softbutton mode “fire” key (center softbutton) is sent to this method in order to allow 3 button devices to work properly. |
protected boolean isSelectableInteraction() | This method allows a component to indicate that it is interested in an “implicit” select command to appear in the “fire” button when 3 softbuttons are defined in a device. |
public void pointerHover(int[] x, int[] y) | Invoked for devices where the pointer can hover without actually clicking the display. |
public void pointerHoverReleased(int[] x, int[] y) | Invoked for devices where the pointer can hover without actually clicking the display. |
public void pointerPressed(int x, int y) | If this Component is focused, the pointer pressed event will call this method |
public void pointerReleased(int x, int y) | If this Component is focused, the pointer released event will call this method |
protected void dragInitiated() | Invoked on the focus component to let it know that drag has started on the parent container for the case of a component that doesn’t support scrolling |
protected Dimension calcPreferredSize() | Calculates the preferred size based on component content. |
protected Border getBorder() | Normally returns getStyle().getBorder() but some subclasses might use this to programmatically replace the border in runtime e.g. for a pressed border effect |
public Command getCommand() | This method return the Button Command if exists |
public void setCommand(Command cmd) | Applies the given command to this button |
public boolean isSelected() | Returns true if the button is selected for toggle buttons, |
public void setAlignment(int align) | Deprecated Sets the Alignment of the Label to one of: CENTER, LEFT, RIGHT |
public boolean isToggle() | Toggle button mode is only relevant for checkboxes/radio buttons. |
public void setToggle(boolean toggle) | Toggle button mode is only relevant for checkboxes/radio buttons. |
public void setUIID(String id) | Overriden to workaround issue with caps text and different UIID’s This method sets the Component the Unique identifier. |
public boolean animate() | Allows the animation to reduce “repaint” calls when it returns false. |
public boolean isOppositeSide() | Places the check box or radio button on the opposite side at the far end |
public int getReleaseRadius() | Indicates a radius in which a pointer release will still have effect. |
public void setReleaseRadius(int releaseRadius) | Indicates a radius in which a pointer release will still have effect. |
public boolean isAutoRelease() | Returns if this is an auto released Button. |
public void setAutoRelease(boolean autoRelease) | Sets the auto released mode of this button, by default it’s not an auto released Button |
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. |
public final boolean isCapsText() | Indicates whether text on the button should be drawn capitalized by default to match the Android design. |
public void setCapsText(boolean capsText) | Indicates whether text on the button should be drawn capitalized by default to match the Android design. |
public void setText(String t) | Overriden to implement the caps mode #setCapsText(boolean) Sets the Label text |
Inherited fields
From Component
DEFAULT_CURSOR, CROSSHAIR_CURSOR, TEXT_CURSOR, WAIT_CURSOR, SW_RESIZE_CURSOR, SE_RESIZE_CURSOR, NW_RESIZE_CURSOR, NE_RESIZE_CURSOR, N_RESIZE_CURSOR, S_RESIZE_CURSOR, W_RESIZE_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, MOVE_CURSOR, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_Y, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_IMMEDIATELY_DRAG_XY, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_CENTER_OFFSET, BRB_OTHER, CENTER, TOP, LEFT, BOTTOM, RIGHT, BASELINE
Inherited methods
From Label
getDefaultGap, setDefaultGap, isDefaultTickerEnabled, setDefaultTickerEnabled, laidOut, getBadgeText, setBadgeText, setBadgeUIID, getBadgeStyleComponent, getIconStyleComponent, setFontIcon, setMaterialIcon, setFontIcon, getMaterialIcon, setMaterialIcon, getFontIcon, setFontIcon, getMaterialIconSize, getFontIconSize, getIconFont, getBaselineResizeBehavior, getBaseline, refreshTheme, initLaf, getText, getIcon, setIcon, getVerticalAlignment, setVerticalAlignment, getAlignment, getTextPosition, setTextPosition, getGap, setGap, paramString, getMaxAutoSize, setMaxAutoSize, getMinAutoSize, setMinAutoSize, getShiftText, setShiftText, shouldTickerStart, startTicker, startTicker, stopTicker, isTickerRunning, isTickerEnabled, setTickerEnabled, isEndsWith3Points, setEndsWith3Points, getMask, setMask, getMaskName, setMaskName, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, getMaskedIcon, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, isShouldLocalize, setShouldLocalize, getShiftMillimeters, setShiftMillimeters, setShiftMillimeters, getShiftMillimetersF, isShowEvenIfBlank, setShowEvenIfBlank, getStringWidth, isLegacyRenderer, setLegacyRenderer, styleChanged, initUnselectedStyle, isAutoSizeMode, setAutoSizeMode, isTextSelectionEnabled, setTextSelectionEnabled, getTextSelectionSupport, getIconUIID, setIconUIID
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
Field details
STATE_ROLLOVER
public static final int STATE_ROLLOVER = 0STATE_PRESSED
public static final int STATE_PRESSED = 1STATE_DEFAULT
public static final int STATE_DEFAULT = 2Constructor details
Button
public Button()Button
public Button(String text)Parameters
textString- label appearing on the button
Button
public Button(Command cmd)Parameters
cmdCommand- command whose text would be used for the button and would recive action events from the button
Button
public Button(Image icon)Parameters
iconImage- appearing on the button
Button
public Button(char icon)Parameters
iconchar- appearing on the button
Button
public Button(String text, char icon, String id)Parameters
textString- label appearing on the button
iconchar- image appearing on the button
idString- UIID unique identifier for button
Button
public Button(String text, char icon, float iconSize, String id)Parameters
textString- label appearing on the button
iconchar- image appearing on the button
iconSizefloat- image size in millimeters
idString- UIID unique identifier for button
Button
public Button(String text, Image icon, String id)Parameters
textString- label appearing on the button
iconImage- image appearing on the button
idString- UIID unique identifier for button
Button
public Button(String text, Image icon)Parameters
textString- label appearing on the button
iconImage- image appearing on the button
Button
public Button(Image icon, String id)Parameters
iconImage- image appearing on the button
idString- UIID unique identifier for button
Button
public Button(char icon, String id)Parameters
iconchar- image appearing on the button
idString- UIID unique identifier for button
Button
public Button(char icon, float iconSize, String id)Parameters
iconchar- image appearing on the button
iconSizefloat- the size of the icon in millimeters
idString- UIID unique identifier for button
Button
public Button(String text, String id)Parameters
textString- label appearing on the button
idString- UIID unique identifier for button
Method details
isCapsTextDefault
public static boolean isCapsTextDefault()capsButtonTextBool theme constantReturns
setCapsTextDefault
public static void setCapsTextDefault(boolean aCapsTextDefault)capsButtonTextBool theme constantParameters
aCapsTextDefaultboolean- the capsTextDefault to set
isButtonRippleEffectDefault
public static boolean isButtonRippleEffectDefault()Returns
setButtonRippleEffectDefault
public static void setButtonRippleEffectDefault(boolean aButtonRippleEffectDefault)Parameters
aButtonRippleEffectDefaultboolean- the buttonRippleEffectDefault to set
bindStateTo
public void bindStateTo(Button button)Parameters
buttonButton- The button whose state to bind to.
unbindStateFrom
public void unbindStateFrom(Button button)Parameters
buttonButton- The button to unbind state from.
resetFocusable
protected void resetFocusable()getState
public int getState()Returns
setReleased
public void setReleased()getPressedIcon
public Image getPressedIcon()Returns
See also
setPressedIcon
public void setPressedIcon(Image pressedIcon)Parameters
pressedIconImage- icon used
See also
getRolloverPressedIcon
public Image getRolloverPressedIcon()Returns
setRolloverPressedIcon
public void setRolloverPressedIcon(Image rolloverPressedIcon)Parameters
rolloverPressedIconImage- icon used
getDisabledIcon
public Image getDisabledIcon()Returns
setDisabledIcon
public void setDisabledIcon(Image disabledIcon)Parameters
disabledIconImage- icon used
getRolloverIcon
public Image getRolloverIcon()Returns
See also
setRolloverIcon
public void setRolloverIcon(Image rolloverIcon)Parameters
rolloverIconImage- icon to use
See also
addStateChangeListener
public void addStateChangeListener(ActionListener<ComponentStateChangeEvent> l)Parameters
lActionListener<ComponentStateChangeEvent>- Listener to be notified when state changes
See also
removeStateChangeListener
public void removeStateChangeListener(ActionListener<ComponentStateChangeEvent> l)Parameters
lActionListener<ComponentStateChangeEvent>- State change listener to remove.
See also
addActionListener
public void addActionListener(ActionListener<ActionEvent> l)Parameters
lActionListener<ActionEvent>- implementation of the action listener interface
removeActionListener
public void removeActionListener(ActionListener<ActionEvent> l)Parameters
lActionListener<ActionEvent>- implementation of the action listener interface
getActionListeners
public Vector<ActionListener<ActionEvent>> getActionListeners()Returns
getListeners
public Collection<ActionListener<ActionEvent>> getListeners()Returns
getIconFromState
public Image getIconFromState()Returns
fireActionEvent
protected void fireActionEvent(int x, int y)Parameters
xint- the x position of the click if applicable (can be 0 or -1 otherwise)
yint- the y position of the click if applicable (can be 0 or -1 otherwise)
pressed
public void pressed()released
public void released()released
public void released(int x, int y)Parameters
xint- the x position if a touch event triggered this, -1 if this isn’t relevant
yint- the y position if a touch event triggered this, -1 if this isn’t relevant
keyPressed
public void keyPressed(int keyCode)Parameters
keyCodeint- the key code value to indicate a physical key.
keyReleased
public void keyReleased(int keyCode)Parameters
keyCodeint- the key code value to indicate a physical key.
keyRepeated
public void keyRepeated(int keyCode)Parameters
keyCodeint- the key code value to indicate a physical key.
fireClicked
protected void fireClicked()isSelectableInteraction
protected boolean isSelectableInteraction()Returns
pointerHover
public void pointerHover(int[] x, int[] y)Parameters
xint[]- the pointer x coordinate
yint[]- the pointer y coordinate
pointerHoverReleased
public void pointerHoverReleased(int[] x, int[] y)Parameters
xint[]- the pointer x coordinate
yint[]- the pointer y coordinate
pointerPressed
public void pointerPressed(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
pointerReleased
public void pointerReleased(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
dragInitiated
protected void dragInitiated()calcPreferredSize
protected Dimension calcPreferredSize()Returns
getBorder
protected Border getBorder()Returns
getCommand
public Command getCommand()Returns
setCommand
public void setCommand(Command cmd)Parameters
cmdCommand- the command on the button
isSelected
public boolean isSelected()Returns
setAlignment
public void setAlignment(int align)Parameters
alignint- alignment value
isToggle
public boolean isToggle()Returns
setToggle
public void setToggle(boolean toggle)Parameters
toggleboolean- the toggle to set
setUIID
public void setUIID(String id)Parameters
idString- UIID unique identifier for component type
animate
public boolean animate()com.codename1.ui.Display class.Returns
isOppositeSide
public boolean isOppositeSide()Returns
getReleaseRadius
public int getReleaseRadius()Returns
setReleaseRadius
public void setReleaseRadius(int releaseRadius)Parameters
releaseRadiusint- the releaseRadius to set
isAutoRelease
public boolean isAutoRelease()Returns
setAutoRelease
public void setAutoRelease(boolean autoRelease)paint
public void paint(Graphics g)Parameters
gGraphics- the component graphics
isCapsText
public final boolean isCapsText()Button and
RaisedButton will be capped to keep compatibility. You can add
additional UIID’s to the mix by using the theme constant
capsButtonUiids which can include a separated list of the
UIID’s to capitalizeReturns
setCapsText
public void setCapsText(boolean capsText)Button and
RaisedButton will be capped to keep compatibility. You can add
additional UIID’s to the mix by using the theme constant
capsButtonUiids which can include a separated list of the
UIID’s to capitalizeParameters
capsTextboolean- the capsText to set
setText
public void setText(String t)#setCapsText(boolean)
Sets the Label textParameters
tString- the string that the label presents.