public class Button extends Label implements ReleasableComponent, ActionSource, SelectableIconHolder
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 ActionListener that react when the
Button is clicked or handle actions via a
Command.
Button has the "Button" UIID by default.
Here is trivial usage of the Button API:
This code shows a common use case of making a button look like a hyperlink

| Modifier and Type | Field and Description |
|---|---|
static int |
STATE_DEFAULT
Indicates the default state of a button which is neither pressed nor focused
|
static int |
STATE_PRESSED
Indicates the pressed state of a button
|
static int |
STATE_ROLLOVER
Indicates the rollover state of a button which is equivalent to focused for
most uses
|
BASELINE, BOTTOM, BRB_CENTER_OFFSET, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_OTHER, CENTER, CROSSHAIR_CURSOR, DEFAULT_CURSOR, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_POSSIBLE_DRAG_Y, E_RESIZE_CURSOR, HAND_CURSOR, LEFT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NW_RESIZE_CURSOR, RIGHT, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, TOP, W_RESIZE_CURSOR, WAIT_CURSOR| Constructor and Description |
|---|
Button()
Constructs a button with an empty string for its text.
|
Button(char icon)
Constructs a button with the specified material image icon.
|
Button(char icon,
float iconSize,
String id)
Constructor a button with material image icon and UIID
|
Button(char icon,
String id)
Constructor a button with material image icon and UIID
|
Button(Command cmd)
Allows binding a command to a button for ease of use
|
Button(Image icon)
Constructs a button with the specified image.
|
Button(Image icon,
String id)
Constructor a button with image and UIID
|
Button(String text)
Constructs a button with the specified text.
|
Button(String text,
char icon,
float iconSize,
String id)
Constructor a button with text, material image and uiid
|
Button(String text,
char icon,
String id)
Constructor a button with text, material image and uiid
|
Button(String text,
Image icon)
Constructor a button with text and image
|
Button(String text,
Image icon,
String id)
Constructor a button with text, image and uiid
|
Button(String text,
String id)
Constructor a button with text and UIID
|
| Modifier and Type | Method and Description |
|---|---|
void |
addActionListener(ActionListener l)
Adds a listener to the button which will cause an event to dispatch on click
|
void |
addStateChangeListener(ActionListener l)
Adds a listener to be notified when the button state changes.
|
boolean |
animate()
Allows the animation to reduce "repaint" calls when it returns false.
|
void |
bindStateTo(Button button)
Bind the state of this button to another button's state.
|
protected Dimension |
calcPreferredSize()
Calculates the preferred size based on component content.
|
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 void |
fireActionEvent(int x,
int y)
Allows subclasses to override action event behavior
|
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.
|
Vector |
getActionListeners()
Deprecated.
use getListeners instead
|
protected Border |
getBorder()
Normally returns getStyle().getBorder() but some subclasses might use this
to programmatically replace the border in runtime e.g.
|
Command |
getCommand()
This method return the Button Command if exists
|
Image |
getDisabledIcon()
Indicates the icon that is displayed on the button when the button is in
the disabled state
|
Image |
getIconFromState()
Returns the icon for the button based on its current state
|
Collection |
getListeners()
Returns a collection containing the action listeners for this button
|
Image |
getPressedIcon()
Indicates the icon that is displayed on the button when the button is in
pressed state
|
int |
getReleaseRadius()
Indicates a radius in which a pointer release will still have effect.
|
Image |
getRolloverIcon()
Indicates the icon that is displayed on the button when the button is in
rolled over state
|
Image |
getRolloverPressedIcon()
Indicates the icon that is displayed on the button when the button is in
pressed state and is selected.
|
int |
getState()
Returns the button state
|
boolean |
isAutoRelease()
Returns if this is an auto released Button.
|
static boolean |
isButtonRippleEffectDefault()
Default value for the button ripple effect, this can be set with the theme constant buttonRippleBool
|
boolean |
isCapsText()
Indicates whether text on the button should be drawn capitalized by
default to match the Android design.
|
static boolean |
isCapsTextDefault()
Indicates whether text on the button should be drawn capitalized by default to match the Android design.
|
boolean |
isOppositeSide()
Places the check box or radio button on the opposite side at the far end
|
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.
|
boolean |
isSelected()
Returns true if the button is selected for toggle buttons,
|
boolean |
isToggle()
Toggle button mode is only relevant for checkboxes/radio buttons.
|
void |
keyPressed(int keyCode)
If this Component is focused, the key pressed event
will call this method
|
void |
keyReleased(int keyCode)
If this Component is focused, the key released event
will call this method
|
void |
keyRepeated(int keyCode)
If this Component is focused, the key repeat event
will call this method.
|
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.
|
void |
pointerHover(int[] x,
int[] y)
Invoked for devices where the pointer can hover without actually clicking
the display.
|
void |
pointerHoverReleased(int[] x,
int[] y)
Invoked for devices where the pointer can hover without actually clicking
the display.
|
void |
pointerPressed(int x,
int y)
If this Component is focused, the pointer pressed event
will call this method
|
void |
pointerReleased(int x,
int y)
If this Component is focused, the pointer released event
will call this method
|
void |
pressed()
Invoked to change the state of the button to the pressed state
|
void |
released()
Invoked to change the state of the button to the released state
|
void |
released(int x,
int y)
Invoked to change the state of the button to the released state
|
void |
removeActionListener(ActionListener l)
Removes the given action listener from the button
|
void |
removeStateChangeListener(ActionListener l)
Removes state change listener.
|
protected void |
resetFocusable()
Restores the state of the focusable flag to its default state
|
void |
setAlignment(int align)
Deprecated.
use the Style alignment instead
|
void |
setAutoRelease(boolean autoRelease)
Sets the auto released mode of this button, by default it's not an auto
released Button
|
static void |
setButtonRippleEffectDefault(boolean aButtonRippleEffectDefault)
Default value for the button ripple effect, this can be set with the theme constant buttonRippleBool
|
void |
setCapsText(boolean capsText)
Indicates whether text on the button should be drawn capitalized by
default to match the Android design.
|
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 |
void |
setCommand(Command cmd)
Applies the given command to this button
|
void |
setDisabledIcon(Image disabledIcon)
Indicates the icon that is displayed on the button when the button is in
the disabled state
|
void |
setPressedIcon(Image pressedIcon)
Indicates the icon that is displayed on the button when the button is in
pressed state
|
void |
setReleased()
Set the button in released and unfocused state
|
void |
setReleaseRadius(int releaseRadius)
Indicates a radius in which a pointer release will still have effect.
|
void |
setRolloverIcon(Image rolloverIcon)
Indicates the icon that is displayed on the button when the button is in
rolled over state
|
void |
setRolloverPressedIcon(Image rolloverPressedIcon)
Indicates the icon that is displayed on the button when the button is in
pressed state and is selected.
|
void |
setText(String t)
Overriden to implement the caps mode
setCapsText(boolean)
Sets the Label text |
void |
setToggle(boolean toggle)
Toggle button mode is only relevant for checkboxes/radio buttons.
|
void |
setUIID(String id)
Overriden to workaround issue with caps text and different UIID's
This method sets the Component the Unique identifier.
|
void |
unbindStateFrom(Button button)
Unbinds the state of this button from another button.
|
bindProperty, getAlignment, getBadgeStyleComponent, getBadgeText, getBaseline, getBaselineResizeBehavior, getBindablePropertyNames, getBindablePropertyTypes, getBoundPropertyValue, getDefaultGap, getFontIcon, getFontIconSize, getGap, getIcon, getIconFont, getIconStyleComponent, getIconUIID, getMask, getMaskedIcon, getMaskName, getMaterialIcon, getMaterialIconSize, getMaxAutoSize, getMinAutoSize, getPropertyNames, getPropertyTypeNames, getPropertyTypes, getPropertyValue, getShiftMillimeters, getShiftMillimetersF, getShiftText, getStringWidth, getText, getTextPosition, getTextSelectionSupport, getVerticalAlignment, initLaf, initUnselectedStyle, isAutoSizeMode, isDefaultTickerEnabled, isEndsWith3Points, isLegacyRenderer, isShouldLocalize, isShowEvenIfBlank, isTextSelectionEnabled, isTickerEnabled, isTickerRunning, laidOut, paramString, refreshTheme, setAutoSizeMode, setBadgeText, setBadgeUIID, setBoundPropertyValue, setDefaultGap, setDefaultTickerEnabled, setEndsWith3Points, setFontIcon, setFontIcon, setFontIcon, setGap, setIcon, setIconUIID, setLegacyRenderer, setMask, setMaskName, setMaterialIcon, setMaterialIcon, setMaxAutoSize, setMinAutoSize, setPropertyValue, setShiftMillimeters, setShiftMillimeters, setShiftText, setShouldLocalize, setShowEvenIfBlank, setTextPosition, setTextSelectionEnabled, setTickerEnabled, setVerticalAlignment, shouldTickerStart, startTicker, startTicker, stopTicker, styleChanged, unbindPropertyaddDragFinishedListener, addDragOverListener, addDropListener, addFocusListener, addLongPressListener, addPointerDraggedListener, addPointerPressedListener, addPointerReleasedListener, addPullToRefresh, addScrollListener, blocksSideSwipe, calcScrollSize, cancelRepaints, clearClientProperties, contains, containsOrOwns, createStyleAnimation, deinitialize, deinitializeCustomStyle, dragEnter, dragExit, dragFinished, draggingOver, drawDraggedImage, drop, focusGained, focusLost, getAbsoluteX, getAbsoluteY, getAllStyles, getAnimationManager, getBottomGap, getBounds, getBounds, getClientProperty, getCloudBoundProperty, getCloudDestinationProperty, getComponentForm, getComponentState, getCursor, getDefaultDragTransparency, getDirtyRegion, getDisabledStyle, getDraggedx, getDraggedy, getDragImage, getDragRegionStatus, getDragSpeed, getDragTransparency, getEditingDelegate, getGridPosX, getGridPosY, getHeight, getInlineAllStyles, getInlineDisabledStyles, getInlinePressedStyles, getInlineSelectedStyles, getInlineStylesTheme, getInlineUnselectedStyles, getInnerHeight, getInnerPreferredH, getInnerPreferredW, getInnerWidth, getInnerX, getInnerY, getLabelForComponent, getName, getNativeOverlay, getNextFocusDown, getNextFocusLeft, getNextFocusRight, getNextFocusUp, getOuterHeight, getOuterPreferredH, getOuterPreferredW, getOuterWidth, getOuterX, getOuterY, getOwner, getParent, getPreferredH, getPreferredSize, getPreferredSizeStr, getPreferredTabIndex, getPreferredW, getPressedStyle, getSameHeight, getSameWidth, getScrollable, getScrollAnimationSpeed, getScrollDimension, getScrollOpacity, getScrollOpacityChangeSpeed, getScrollX, getScrollY, getSelectCommandText, getSelectedRect, getSelectedStyle, getSideGap, getStyle, getTabIndex, getTensileLength, getTooltip, getUIID, getUIManager, getUnselectedStyle, getVisibleBounds, getVisibleBounds, getWidth, getX, getY, growShrink, handlesInput, hasFixedPreferredSize, hasFocus, hideNativeOverlay, initComponent, initCustomStyle, initDisabledStyle, initPressedStyle, initSelectedStyle, installDefaultPainter, isAlwaysTensile, isBlockLead, isCellRenderer, isChildOf, isDragActivated, isDragAndDropOperation, isDraggable, isDragRegion, isDropTarget, isEditable, isEditing, isEnabled, isFlatten, isFocusable, isGrabsPointerEvents, isHidden, isHidden, isHideInLandscape, isHideInPortrait, isIgnorePointerEvents, isInClippingRegion, isInitialized, isOpaque, isOwnedBy, isPinchBlocksDragAndDrop, isRippleEffect, isRTL, isScrollable, isScrollableX, isScrollableY, isScrollVisible, isSetCursorSupported, isSmoothScrolling, isSnapToGrid, isStickyDrag, isTactileTouch, isTactileTouch, isTensileDragEnabled, isTraversable, isVisible, longKeyPress, longPointerPress, onScrollX, onScrollY, paintBackground, paintBackgrounds, paintBorder, paintBorderBackground, paintComponent, paintComponent, paintIntersectingComponentsAbove, paintLock, paintLockRelease, paintRippleOverlay, paintScrollbars, paintScrollbarX, paintScrollbarY, paintShadows, parsePreferredSize, pinch, pinch, pinchReleased, pointerDragged, pointerDragged, pointerHoverPressed, pointerPressed, pointerReleased, putClientProperty, refreshTheme, refreshTheme, remove, removeDragFinishedListener, removeDragOverListener, removeDropListener, removeFocusListener, removeLongPressListener, removePointerDraggedListener, removePointerPressedListener, removePointerReleasedListener, removeScrollListener, repaint, repaint, requestFocus, respondsToPointerEvents, scrollRectToVisible, scrollRectToVisible, setAlwaysTensile, setBlockLead, setCellRenderer, setCloudBoundProperty, setCloudDestinationProperty, setComponentState, setCursor, setDefaultDragTransparency, setDirtyRegion, setDisabledStyle, setDraggable, setDragTransparency, setDropTarget, setEditingDelegate, setEnabled, setFlatten, setFocus, setFocusable, setGrabsPointerEvents, setHandlesInput, setHeight, setHidden, setHidden, setHideInLandscape, setHideInPortrait, setIgnorePointerEvents, setInitialized, setInlineAllStyles, setInlineDisabledStyles, setInlinePressedStyles, setInlineSelectedStyles, setInlineStylesTheme, setInlineUnselectedStyles, setIsScrollVisible, setLabelForComponent, setName, setNextFocusDown, setNextFocusLeft, setNextFocusRight, setNextFocusUp, setOpaque, setOwner, setPinchBlocksDragAndDrop, setPreferredH, setPreferredSize, setPreferredSizeStr, setPreferredTabIndex, setPreferredW, setPressedStyle, setRippleEffect, setRTL, setSameHeight, setSameSize, setSameWidth, setScrollAnimationSpeed, setScrollOpacityChangeSpeed, setScrollSize, setScrollVisible, setScrollX, setScrollY, setSelectCommandText, setSelectedStyle, setShouldCalcPreferredSize, setSize, setSmoothScrolling, setSnapToGrid, setTabIndex, setTactileTouch, setTensileDragEnabled, setTensileLength, setTooltip, setTraversable, setUIID, setUnselectedStyle, setVisible, setWidth, setX, setY, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, shouldRenderComponentSelection, showNativeOverlay, startEditingAsync, stopEditing, stripMarginAndPadding, toImage, toString, updateNativeOverlay, visibleBoundsContainsclone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitgetGap, getIcon, getIconStyleComponent, getIconUIID, getTextPosition, setFontIcon, setGap, setIcon, setIconUIID, setMaterialIcon, setTextPositionpublic static final int STATE_ROLLOVER
public static final int STATE_PRESSED
public static final int STATE_DEFAULT
public Button()
public Button(String text)
text - label appearing on the buttonpublic Button(Command cmd)
cmd - command whose text would be used for the button and would recive action events
from the buttonpublic Button(Image icon)
icon - appearing on the buttonpublic Button(char icon)
icon - appearing on the buttonpublic Button(String text, char icon, String id)
text - label appearing on the buttonicon - image appearing on the buttonid - UIID unique identifier for buttonpublic Button(String text, char icon, float iconSize, String id)
text - label appearing on the buttonicon - image appearing on the buttoniconSize - image size in millimetersid - UIID unique identifier for buttonpublic Button(String text, Image icon, String id)
text - label appearing on the buttonicon - image appearing on the buttonid - UIID unique identifier for buttonpublic Button(String text, Image icon)
text - label appearing on the buttonicon - image appearing on the buttonpublic Button(Image icon, String id)
icon - image appearing on the buttonid - UIID unique identifier for buttonpublic Button(char icon,
String id)
icon - image appearing on the buttonid - UIID unique identifier for buttonpublic Button(char icon,
float iconSize,
String id)
icon - image appearing on the buttoniconSize - the size of the icon in millimetersid - UIID unique identifier for buttonpublic static boolean isCapsTextDefault()
capsButtonTextBool theme constantpublic static void setCapsTextDefault(boolean aCapsTextDefault)
capsButtonTextBool theme constantaCapsTextDefault - the capsTextDefault to setpublic static boolean isButtonRippleEffectDefault()
public static void setButtonRippleEffectDefault(boolean aButtonRippleEffectDefault)
aButtonRippleEffectDefault - the buttonRippleEffectDefault to setpublic void setCommand(Command cmd)
cmd - the command on the buttonpublic void bindStateTo(Button button)
button - The button whose state to bind to.unbindStateFrom(com.codename1.ui.Button)public void unbindStateFrom(Button button)
button - The button to unbind state from.bindStateTo(com.codename1.ui.Button)protected void resetFocusable()
resetFocusable in class Componentpublic int getState()
public void setReleased()
setReleased in interface ReleasableComponentpublic Image getPressedIcon()
getPressedIcon in interface SelectableIconHolderSTATE_PRESSEDpublic Image getRolloverPressedIcon()
getRolloverPressedIcon in interface SelectableIconHolderpublic void setRolloverPressedIcon(Image rolloverPressedIcon)
setRolloverPressedIcon in interface SelectableIconHolderrolloverPressedIcon - icon usedpublic Image getDisabledIcon()
getDisabledIcon in interface SelectableIconHolderpublic Image getRolloverIcon()
getRolloverIcon in interface SelectableIconHolderSTATE_ROLLOVERpublic void setRolloverIcon(Image rolloverIcon)
setRolloverIcon in interface SelectableIconHolderrolloverIcon - icon to useSTATE_ROLLOVERpublic void setPressedIcon(Image pressedIcon)
setPressedIcon in interface SelectableIconHolderpressedIcon - icon usedSTATE_PRESSEDpublic void setDisabledIcon(Image disabledIcon)
setDisabledIcon in interface SelectableIconHolderdisabledIcon - icon usedpublic void addStateChangeListener(ActionListener l)
addStateChangeListener in class Componentl - Listener to be notified when state changesgetState(),
setState(int),
removeStateChangeListener(com.codename1.ui.events.ActionListener)public void removeStateChangeListener(ActionListener l)
removeStateChangeListener in class Componentl - State change listener to remove.addStateChangeListener(com.codename1.ui.events.ActionListener),
getState(),
setState(int)public void addActionListener(ActionListener l)
addActionListener in interface ActionSourcel - implementation of the action listener interfacepublic void removeActionListener(ActionListener l)
removeActionListener in interface ActionSourcel - implementation of the action listener interfacepublic Vector getActionListeners()
public Collection getListeners()
public Image getIconFromState()
getIconFromState in interface SelectableIconHolderprotected void fireActionEvent(int x,
int y)
x - the x position of the click if applicable (can be 0 or -1 otherwise)y - the y position of the click if applicable (can be 0 or -1 otherwise)public void pressed()
public void released()
public void released(int x,
int y)
x - the x position if a touch event triggered this, -1 if this isn't relevanty - the y position if a touch event triggered this, -1 if this isn't relevantpublic void keyPressed(int keyCode)
keyPressed in class ComponentkeyCode - the key code value to indicate a physical key.public void keyReleased(int keyCode)
keyReleased in class ComponentkeyCode - the key code value to indicate a physical key.public void keyRepeated(int keyCode)
keyRepeated in class ComponentkeyCode - the key code value to indicate a physical key.protected void fireClicked()
fireClicked in class Componentprotected boolean isSelectableInteraction()
isSelectableInteraction in class Componentpublic void pointerHover(int[] x,
int[] y)
pointerHover in class Componentx - the pointer x coordinatey - the pointer y coordinatepublic void pointerHoverReleased(int[] x,
int[] y)
pointerHoverReleased in class Componentx - the pointer x coordinatey - the pointer y coordinatepublic void pointerPressed(int x,
int y)
pointerPressed in class Componentx - the pointer x coordinatey - the pointer y coordinatepublic void pointerReleased(int x,
int y)
pointerReleased in class Componentx - the pointer x coordinatey - the pointer y coordinateprotected void dragInitiated()
dragInitiated in class Componentprotected Dimension calcPreferredSize()
calcPreferredSize in class Labelprotected Border getBorder()
public Command getCommand()
public boolean isSelected()
public void setAlignment(int align)
setAlignment in class Labelalign - alignment valueComponent.CENTER,
Component.LEFT,
Component.RIGHTpublic boolean isToggle()
public void setToggle(boolean toggle)
toggle - the toggle to setpublic void setUIID(String id)
public boolean animate()
Display class.public boolean isOppositeSide()
public int getReleaseRadius()
getReleaseRadius in interface ReleasableComponentpublic void setReleaseRadius(int releaseRadius)
setReleaseRadius in interface ReleasableComponentreleaseRadius - the releaseRadius to setpublic boolean isAutoRelease()
isAutoRelease in interface ReleasableComponentpublic void setAutoRelease(boolean autoRelease)
setAutoRelease in interface ReleasableComponentpublic void paint(Graphics g)
Labelpublic 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 capitalizepublic 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 capitalizecapsText - the capsText to setpublic void setText(String t)
setCapsText(boolean)
Sets the Label textsetText in interface TextHoldersetText in class Labelt - the string that the label presents.