public class Button extends Control implements TextControl
Here is an example showing a push button being used:
public class MyProgram extends MainWindow { Button btn1,btn2; public void initUI() { add(btn1 = new Button("Push me\nPlease"), CENTER,TOP); add(btn2 = new Button(new Image("myimage.png")), CENTER,AFTER+3); } public void onEvent(Event event) { if (event.type == ControlEvent.PRESSED) { if (event.target == btn1) { ... handle btn1 being pressed
Control.TranslucentShape
Modifier and Type | Field and Description |
---|---|
protected boolean |
armed |
int |
AUTO_DELAY
The frequency in which the PRESSED event will be posted after the
INITIAL_DELAY was reached.
|
boolean |
autoRepeat
Set to true to enable auto-repeat feature for this button.
|
protected byte |
border |
static byte |
BORDER_3D
Specifies a 3d border for this button.
|
static byte |
BORDER_3D_HORIZONTAL_GRADIENT
Specifies a horizontal 3d-gradient border for this button.
|
static byte |
BORDER_3D_VERTICAL_GRADIENT
Specifies a vertical 3d-gradient border for this button.
|
static byte |
BORDER_GRAY_IMAGE
Specifies that the image passed in the constructor is a gray image that will
be recolorized and resized to the text's size.
|
static byte |
BORDER_NONE
Specifies no border for this button.
|
static byte |
BORDER_OUTLINED
Specifies a outlined border.
|
static byte |
BORDER_ROUND
Specifies a rounded border for this button.
|
static byte |
BORDER_SIMPLE
Specifies a single-lined border for this button.
|
int |
borderColor
The border color.
|
int |
borderColor3DG
The border color of this button when BORDER_3D_GRADIENT border style is
active.
|
int |
borderWidth3DG
The border width of this button when BORDER_3D_GRADIENT border style is
active.
|
int |
bottomColor3DG
The bottom color of this button when BORDER_3D_GRADIENT border style is
active.
|
static int |
CENTRALIZE |
static int |
commonGap
Set commonGap to a value to make all further buttons with the same internal
gap.
|
int |
cornerRadius3DG
The corner radius of this button when BORDER_3D_GRADIENT border style is
active.
|
int |
currentFrame
The current frame in a multi-frame image.
|
int |
disabledColor
Set the color for the control when it's disabled.
|
boolean |
drawBordersIfTransparentBackground
Set to true to draw the button borders if transparentBackground is true.
|
protected int |
fColor |
boolean |
fillWhenPressedOnTransparentBackground
Fills the button when pressed even if transparentBackground is set.
|
boolean |
flatBackground
If ui style is vista and border is BORDER_NONE, setting this to false will
make the button have a vista-like background.
|
protected int[] |
fourColors |
int |
hightlightColor
Deprecated.
Fix the typo, use Control.textShadowColor instead.
|
int |
imageHeightFactor
The height of the image based on the button's height, ranging from 1 to 100.
|
protected Image |
img |
protected Image |
img0 |
protected Image |
imgDis |
int |
INITIAL_DELAY
The initial delay to start the auto-repeat.
|
boolean |
isSticky
Set to true to make this button sticky: it keeps pressed until the next
click.
|
protected int |
ix0 |
protected int |
iy0 |
int |
paddingBottom
Sets the bottom padding for this button.
|
int |
paddingLeft
Sets the left padding for this button.
|
int |
paddingRight
Sets the right padding for this button.
|
int |
paddingTop
Sets the top padding for this button.
|
protected int |
pressColor |
Image |
pressedImage
Sets the image that will be displayed when the user press this button.
|
java.lang.String |
relativeToText
Used when the textPosition is RIGHT_OF.
|
int |
roundBorderFactor
The factor by which the height will be divided to find the round border
radius.
|
boolean |
shiftOnPress
Set to false to disable the button's shift when its pressed.
|
protected java.lang.String |
text |
int |
topColor3DG
The top color of this button when BORDER_3D_GRADIENT border style is active.
|
protected int |
tx0 |
protected int |
ty0 |
boolean |
underlinedText
Set to true to put a line under the text.
|
AFTER, alphaValue, appId, appObj, asContainer, asWindow, backColor, BEFORE, BOTTOM, BOTTOM_OF, BRIGHTER_BACKGROUND, callListenersOnAllTargets, CENTER, CENTER_OF, clearValueInt, clearValueStr, DARKER_BACKGROUND, DP, effect, enableUpdateScreen, esce, eventsEnabled, FILL, fillColor, FIT, floating, fm, fmH, focusHandler, focusLess, focusOnPenDown, focusTraversable, font, FONTSIZE, foreColor, height, ignoreInsets, isHighlighting, isTablet, KEEP, keepDisabled, keepEnabled, LEFT, next, nextTabControl, npParts, offscreen, offscreen0, onEventFirst, parent, PARENTSIZE, PARENTSIZEMAX, PARENTSIZEMIN, PREFERRED, prev, RANGE, repositionAllowed, RIGHT, RIGHT_OF, SAME, SCREENSIZE, SCREENSIZEMAX, SCREENSIZEMIN, setFont, setH, setRel, setW, setX, SETX_NOT_SET, setY, tempW, textShadowColor, TOP, translucentShape, transparentBackground, uiAdjustmentsBasedOnFontHeightIsSupported, uiAndroid, UICONST, uiFlat, uiHolo, uiMaterial, uiVista, visible, width, WILL_RESIZE, x, y
Modifier | Constructor and Description |
---|---|
protected |
Button() |
|
Button(Image img)
Creates a button with the given image.
|
|
Button(Image img,
byte border)
Creates a button with the given image and border.
|
|
Button(java.lang.String text)
Creates a button displaying the given text.
|
|
Button(java.lang.String text,
byte border)
Creates a button displaying the given text and border.
|
|
Button(java.lang.String text,
Image img,
int textPosition,
int gap)
Creates a button that shows the given text and image.
|
Modifier and Type | Method and Description |
---|---|
Image |
getImage()
Returns the image that is assigned to this Button, or null if none.
|
int |
getPreferredHeight()
Returns the preffered height of this control.
|
int |
getPreferredWidth()
Returns the preffered width of this control.
|
java.lang.String |
getText()
Gets the text displayed in the button.
|
boolean |
isPressed()
Returns true if the button is pressed or not.
|
protected void |
onBoundsChanged(boolean screenChanged)
Called after a setRect.
|
protected void |
onColorsChanged(boolean colorsChanged)
Called after a setEnabled, setForeColor and setBackColor and when a control has
been added to a Container.
|
void |
onEvent(Event event)
Called by the system to pass events to the button.
|
protected void |
onFontChanged()
Called after a setFont
|
void |
onPaint(Graphics g)
Called by the system to draw the button.
|
protected void |
paintBackground(Graphics g)
Paint button's background.
|
protected void |
paintImage(Graphics g,
boolean bkg,
int ix,
int iy) |
protected void |
paintText(Graphics g,
int tx,
int ty)
Paint button's text.
|
void |
press(boolean pressed)
Simulate the press or release of this button.
|
void |
setBorder(byte border)
Sets the style of the border.
|
void |
setImage(Image img)
Sets the image that is displayed in the button.
|
void |
setNinePatch(Image img)
Set the NinePatch of the control.
|
void |
setNinePatch(Image img,
int corner,
int side)
Set the NinePatch of the control.
|
void |
setPressedColor(int newColor)
Sets the color that the button's background will go ('armed color') when the
button gets a PENDOWN event.
|
void |
setText(java.lang.String text)
Sets the text that is displayed in the button.
|
void |
simulatePress()
Press and depress this Button to simulate that the user had clicked on it.
|
_onEvent, addEnabledStateListener, addFocusListener, addFontChangeHandler, addGridListener, addHandler, addHighlightListener, addKeyListener, addListContainerListener, addMouseListener, addMultiTouchListener, addPenListener, addPressListener, addPushNotificationListener, addSizeChangeHandler, addTimer, addTimer, addTimerListener, addValueChangeHandler, addWindowListener, bringToFront, changeHighlighted, clear, contains, drawTranslucentBackground, getAbsoluteRect, getBackColor, getDoEffect, getEffectH, getEffectW, getEffectX, getEffectY, getEventListeners, getFont, getForeColor, getGap, getGraphics, getHeight, getNext, getParent, getParentWindow, getPos, getPressedEvent, getPrev, getRect, getSize, getTextShadowColor, getWidth, getX, getX2, getY, getY2, hadParentScrolled, handleGeographicalFocusChangeKeys, hasFocus, internalSetEnabled, intXYWH, isActionEvent, isChildOf, isDisplayed, isEnabled, isFloating, isInsideOrNear, isObscured, isTopMost, isVisible, isVisibleAndInside, onWindowPaintFinished, post, postEvent, postPressedEvent, releaseScreenShot, removeEnabledStateListener, removeFocusListener, removeGridListener, removeHandler, removeHighlightListener, removeKeyListener, removeListContainerListener, removeMouseListener, removeMultiTouchListener, removePenListener, removePressListener, removePushNotificationListener, removeTimer, removeTimerListener, removeWindowListener, repaint, repaintNow, reposition, reposition, repositionChildren, requestFocus, resetSetPositions, resetStyle, safeRepaintNow, safeUpdateScreen, sendToBack, setBackColor, setBackForeColors, setDoEffect, setEnabled, setFloating, setFocusLess, setFont, setForeColor, setRect, setRect, setRect, setRect, setSet, setTextShadowColor, setTranslucent, setVisible, showTip, takeInitialScreenShot, takeScreenShot, translateFromOrigin, uiStyleChanged, updateScreen, updateTemporary, willOpenKeyboard
public static final byte BORDER_NONE
public static final byte BORDER_SIMPLE
public static final byte BORDER_3D
public static final byte BORDER_3D_VERTICAL_GRADIENT
public static final byte BORDER_3D_HORIZONTAL_GRADIENT
public static final byte BORDER_GRAY_IMAGE
Important: you must set the button colors before calling setBorder. Here's a sample:
Button btn = new Button("Bárbara\nHazan", new Image("button1.png"), CENTER, 8); btn.setBackColor(backColor); btn.borderColor3DG = 0x008800; // used to colorize the image btn.setFont(font.asBold()); btn.setBorder(Button.BORDER_GRAY_IMAGE); add(btn, CENTER, CENTER);
public static final byte BORDER_ROUND
roundBorderFactor
,
Constant Field Valuespublic static final byte BORDER_OUTLINED
public static final int CENTRALIZE
public boolean drawBordersIfTransparentBackground
public int currentFrame
public boolean shiftOnPress
public int roundBorderFactor
public int disabledColor
public boolean autoRepeat
INITIAL_DELAY
,
AUTO_DELAY
public int INITIAL_DELAY
public int AUTO_DELAY
protected java.lang.String text
protected Image img
protected Image img0
protected Image imgDis
protected boolean armed
protected byte border
protected int tx0
protected int ty0
protected int ix0
protected int iy0
protected int fColor
protected int pressColor
protected int[] fourColors
public int borderColor
public int paddingLeft
public int paddingRight
public int paddingTop
public int paddingBottom
public Image pressedImage
Image img = btn.getImage(); img = img.getFrameInstance(0); // gets a copy of the image img.applyColor(Color.RED); // colorize as red btn.pressedImage = img; // another option: btn.pressedImage = btn.getImage().getFadedInstance(Color.RED);
public boolean flatBackground
public int cornerRadius3DG
public int borderWidth3DG
public int borderColor3DG
public int topColor3DG
public int bottomColor3DG
@Deprecated public int hightlightColor
public static int commonGap
public boolean isSticky
public java.lang.String relativeToText
public boolean underlinedText
Button btLink; btLink = new Button("(Guide 124...)"); btLink.underlinedText = true; btLink.setBorder(BORDER_NONE); btLink.setForeColor(Color.BLUE); add(btLink, LEFT, TOP, FILL, PREFERRED);
public int imageHeightFactor
public boolean fillWhenPressedOnTransparentBackground
public Button(java.lang.String text, Image img, int textPosition, int gap)
text
- The text to be displayedimg
- The image to be displayedtextPosition
- Where to place the text (supports only LEFT, TOP, RIGHT,
BOTTOM, CENTER - no adjustments!). Also supports
RIGHT_OF, which uses the relativeToText field as another
button to be used as reference to be able to center the
title among a set of buttons; the text will be placed at
the right)gap
- The space between the text and the imagepublic Button(java.lang.String text)
public Button(java.lang.String text, byte border)
public Button(Image img)
public Button(Image img, byte border)
protected Button()
public void setText(java.lang.String text)
setText
in interface TextControl
public void setImage(Image img)
public void setPressedColor(int newColor)
newColor
- New color to set as the background when pressed.public void setBorder(byte border)
public java.lang.String getText()
getText
in interface TextControl
public int getPreferredWidth()
getPreferredWidth
in class Control
public int getPreferredHeight()
getPreferredHeight
in class Control
public void simulatePress()
isSticky
public boolean isPressed()
isSticky
is true. You can change the button state
programatically using simulatePress.simulatePress()
,
isSticky
public void onEvent(Event event)
public void press(boolean pressed)
public void setNinePatch(Image img, int corner, int side)
Control
setNinePatch
in class Control
img
- The image to make the NinePatch.
The image must be on a NinePatch format.corner
- The size, in pixels, of the NinePatch's cornerside
- The size, in pixels, of the NinePatch's side.public void setNinePatch(Image img)
Control
setNinePatch
in class Control
img
- The image with guides to make the NinePatch.
The image must be on a NinePatch format.public void onPaint(Graphics g)
protected void onFontChanged()
Control
onFontChanged
in class Control
protected void onBoundsChanged(boolean screenChanged)
Control
onBoundsChanged
in class Control
screenChanged
- If the bounds were changed due to a screen change (rotation, collapse)protected void onColorsChanged(boolean colorsChanged)
Control
onColorsChanged
in class Control
protected void paintBackground(Graphics g)
protected void paintText(Graphics g, int tx, int ty)
protected void paintImage(Graphics g, boolean bkg, int ix, int iy)
public Image getImage()