Package totalcross.ui
Class Button
- java.lang.Object
-
- totalcross.ui.gfx.GfxSurface
-
- totalcross.ui.Control
-
- totalcross.ui.Button
-
- All Implemented Interfaces:
TextControl
- Direct Known Subclasses:
ArrowButton,ShadedButton
public class Button extends Control implements TextControl
Button is a push button control. It supports multilined text, a standalone image, or text mixed with images. Image buttons have a fade look when not enabled. If you want a sticky button, set the isSticky field or use the PushButtonGroup class selecting the CHECK mode.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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class totalcross.ui.Control
Control.TranslucentShape
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanarmedintAUTO_DELAYThe frequency in which the PRESSED event will be posted after the INITIAL_DELAY was reached.booleanautoRepeatSet to true to enable auto-repeat feature for this button.protected byteborderstatic byteBORDER_3DSpecifies a 3d border for this button.static byteBORDER_3D_HORIZONTAL_GRADIENTSpecifies a horizontal 3d-gradient border for this button.static byteBORDER_3D_VERTICAL_GRADIENTSpecifies a vertical 3d-gradient border for this button.static byteBORDER_GRAY_IMAGESpecifies that the image passed in the constructor is a gray image that will be recolorized and resized to the text's size.static byteBORDER_NONESpecifies no border for this button.static byteBORDER_ROUNDSpecifies a rounded border for this button.static byteBORDER_SIMPLESpecifies a single-lined border for this button.intborderColor3DGThe border color of this button when BORDER_3D_GRADIENT border style is active.intborderWidth3DGThe border width of this button when BORDER_3D_GRADIENT border style is active.intbottomColor3DGThe bottom color of this button when BORDER_3D_GRADIENT border style is active.static intcommonGapSet commonGap to a value to make all further buttons with the same internal gap.intcornerRadius3DGThe corner radius of this button when BORDER_3D_GRADIENT border style is active.intcurrentFrameThe current frame in a multi-frame image.booleandrawBordersIfTransparentBackgroundSet to true to draw the button borders if transparentBackground is true.protected intfColorbooleanfillWhenPressedOnTransparentBackgroundFills the button when pressed even if transparentBackground is set.booleanflatBackgroundIf ui style is vista and border is BORDER_NONE, setting this to false will make the button have a vista-like background.protected int[]fourColorsinthightlightColorDeprecated.Fix the typo, use Control.textShadowColor instead.intimageHeightFactorThe height of the image based on the button's height, ranging from 1 to 100.protected Imageimgprotected Imageimg0protected ImageimgDisintINITIAL_DELAYThe initial delay to start the auto-repeat.booleanisStickySet to true to make this button sticky: it keeps pressed until the next click.protected intix0protected intiy0protected intpressColorImagepressedImageSets the image that will be displayed when the user press this button.java.lang.StringrelativeToTextUsed when the textPosition is RIGHT_OF.introundBorderFactorThe factor by which the height will be divided to find the round border radius.booleanshiftOnPressSet to false to disable the button's shift when its pressed.protected java.lang.StringtextinttopColor3DGThe top color of this button when BORDER_3D_GRADIENT border style is active.protected inttx0protected intty0booleanunderlinedTextSet to true to put a line under the text.-
Fields inherited from class totalcross.ui.Control
AFTER, alphaValue, appId, appObj, asContainer, asWindow, backColor, BEFORE, BOTTOM, BOTTOM_OF, BRIGHTER_BACKGROUND, callListenersOnAllTargets, CENTER, CENTER_OF, clearValueInt, clearValueStr, DARKER_BACKGROUND, effect, enableUpdateScreen, esce, eventsEnabled, FILL, fillColor, FIT, fm, fmH, focusHandler, focusLess, focusOnPenDown, focusTraversable, font, FONTSIZE, foreColor, height, ignoreInsets, isHighlighting, isTablet, KEEP, keepDisabled, keepEnabled, LEFT, nextTabControl, offscreen, offscreen0, onEventFirst, parent, PARENTSIZE, PARENTSIZEMAX, PARENTSIZEMIN, PREFERRED, RANGE, repositionAllowed, RIGHT, RIGHT_OF, SAME, SCREENSIZE, SCREENSIZEMAX, SCREENSIZEMIN, setFont, setH, setRel, setW, setX, setY, tempW, textShadowColor, TOP, translucentShape, transparentBackground, uiAdjustmentsBasedOnFontHeightIsSupported, uiAndroid, UICONST, uiFlat, uiHolo, uiMaterial, uiVista, visible, width, WILL_RESIZE, x, y
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedButton()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.Button(Image img)Creates a button with the given image.Button(Image img, byte border)Creates a button with the given image and border.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ImagegetImage()Returns the image that is assigned to this Button, or null if none.intgetPreferredHeight()Returns the preffered height of this control.intgetPreferredWidth()Returns the preffered width of this control.java.lang.StringgetText()Gets the text displayed in the button.booleanisPressed()Returns true if the button is pressed or not.protected voidonBoundsChanged(boolean screenChanged)Called after a setRect.protected voidonColorsChanged(boolean colorsChanged)Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container.voidonEvent(Event event)Called by the system to pass events to the button.protected voidonFontChanged()Called after a setFontvoidonPaint(Graphics g)Called by the system to draw the button.protected voidpaintBackground(Graphics g)Paint button's background.protected voidpaintImage(Graphics g, boolean bkg, int ix, int iy)protected voidpaintText(Graphics g, int tx, int ty)Paint button's text.voidpress(boolean pressed)Simulate the press or release of this button.voidsetBorder(byte border)Sets the style of the border.voidsetImage(Image img)Sets the image that is displayed in the button.voidsetPressedColor(int newColor)Sets the color that the button's background will go ('armed color') when the button gets a PENDOWN event.voidsetText(java.lang.String text)Sets the text that is displayed in the button.voidsimulatePress()Press and depress this Button to simulate that the user had clicked on it.-
Methods inherited from class totalcross.ui.Control
_onEvent, addEnabledStateListener, addFocusListener, addGridListener, addHighlightListener, addKeyListener, addListContainerListener, addMouseListener, addMultiTouchListener, addPenListener, addPressListener, addPushNotificationListener, addTimer, addTimer, addTimerListener, addWindowListener, bringToFront, changeHighlighted, clear, contains, drawTranslucentBackground, getAbsoluteRect, getBackColor, 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, isInsideOrNear, isObscured, isTopMost, isVisible, isVisibleAndInside, onWindowPaintFinished, post, postEvent, postPressedEvent, releaseScreenShot, removeEnabledStateListener, removeFocusListener, removeGridListener, removeHighlightListener, removeKeyListener, removeListContainerListener, removeMouseListener, removeMultiTouchListener, removePenListener, removePressListener, removePushNotificationListener, removeTimer, removeTimerListener, removeWindowListener, repaint, repaintNow, reposition, reposition, repositionChildren, requestFocus, resetSetPositions, safeRepaintNow, safeUpdateScreen, sendToBack, setBackColor, setBackForeColors, setEnabled, setFocusLess, setFont, setForeColor, setRect, setRect, setRect, setRect, setSet, setTextShadowColor, setTranslucent, setVisible, showTip, takeInitialScreenShot, takeScreenShot, translateFromOrigin, uiStyleChanged, updateScreen, updateTemporary, willOpenKeyboard
-
-
-
-
Field Detail
-
BORDER_NONE
public static final byte BORDER_NONE
Specifies no border for this button. Used in the setBorder method.- See Also:
- Constant Field Values
-
BORDER_SIMPLE
public static final byte BORDER_SIMPLE
Specifies a single-lined border for this button. Used in the setBorder method.- See Also:
- Constant Field Values
-
BORDER_3D
public static final byte BORDER_3D
Specifies a 3d border for this button. Used in the setBorder method.- See Also:
- Constant Field Values
-
BORDER_3D_VERTICAL_GRADIENT
public static final byte BORDER_3D_VERTICAL_GRADIENT
Specifies a vertical 3d-gradient border for this button. Used in the setBorder method. Note that, in this mode, the back and fore colors are set using the borderColor3DG, topColor3DG and bottomColor3DG properties.- See Also:
- Constant Field Values
-
BORDER_3D_HORIZONTAL_GRADIENT
public static final byte BORDER_3D_HORIZONTAL_GRADIENT
Specifies a horizontal 3d-gradient border for this button. Used in the setBorder method. Note that, in this mode, the back and fore colors are set using the borderColor3DG, topColor3DG and bottomColor3DG properties.- See Also:
- Constant Field Values
-
BORDER_GRAY_IMAGE
public static final 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. To create the image that will work with this border type, do this:- Get an empty image from somewhere in the web (this site is a good place; choose a light background image to prevent problems described in step 5).
- Do not resize the button.
- Convert the button to grayscale (you can download irfanview and choose menu Image / Convert to Grayscale
- When saving the image (as png), don't forget to save the transparent color (irfanview has an option to save it in the save dialog).
- Images with a smooth round border should have the background colorized to the target background color; otherwise, the results will not be perfect.
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);- Since:
- TotalCross 1.12
- See Also:
- Constant Field Values
-
BORDER_ROUND
public static final byte BORDER_ROUND
Specifies a rounded border for this button. Used in the setBorder method. Note that you MUST use PREFERRED when specifying button's height.- Since:
- TotalCross 3.04
- See Also:
roundBorderFactor, Constant Field Values
-
drawBordersIfTransparentBackground
public boolean drawBordersIfTransparentBackground
Set to true to draw the button borders if transparentBackground is true.- Since:
- TotalCross 1.15
-
currentFrame
public int currentFrame
The current frame in a multi-frame image.
-
shiftOnPress
public boolean shiftOnPress
Set to false to disable the button's shift when its pressed.
-
roundBorderFactor
public int roundBorderFactor
The factor by which the height will be divided to find the round border radius. Used when border is set to BORDER_ROUND.
-
autoRepeat
public boolean autoRepeat
Set to true to enable auto-repeat feature for this button. The PRESSED event will be sent while this button is held.- Since:
- TotalCross 1.22
- See Also:
INITIAL_DELAY,AUTO_DELAY
-
INITIAL_DELAY
public int INITIAL_DELAY
The initial delay to start the auto-repeat. Defaults to 600ms.
-
AUTO_DELAY
public int AUTO_DELAY
The frequency in which the PRESSED event will be posted after the INITIAL_DELAY was reached. Defaults to 150ms.
-
text
protected java.lang.String text
-
img
protected Image img
-
img0
protected Image img0
-
imgDis
protected Image imgDis
-
armed
protected boolean armed
-
border
protected byte border
-
tx0
protected int tx0
-
ty0
protected int ty0
-
ix0
protected int ix0
-
iy0
protected int iy0
-
fColor
protected int fColor
-
pressColor
protected int pressColor
-
fourColors
protected int[] fourColors
-
pressedImage
public Image pressedImage
Sets the image that will be displayed when the user press this button. Only works on Imaged buttons. Here's a sample: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);
- Since:
- TotalCross 1.15
-
flatBackground
public 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. Otherwise, it will have a flat background.- Since:
- TotalCross 1.0 beta 4.
-
cornerRadius3DG
public int cornerRadius3DG
The corner radius of this button when BORDER_3D_GRADIENT border style is active. Set to 10 when setBorder is called.
-
borderWidth3DG
public int borderWidth3DG
The border width of this button when BORDER_3D_GRADIENT border style is active. Set to 2 when setBorder is called.
-
borderColor3DG
public int borderColor3DG
The border color of this button when BORDER_3D_GRADIENT border style is active. Set to 0x00108A when setBorder is called.
-
topColor3DG
public int topColor3DG
The top color of this button when BORDER_3D_GRADIENT border style is active. Set to 0xDCDCFF when setBorder is called.
-
bottomColor3DG
public int bottomColor3DG
The bottom color of this button when BORDER_3D_GRADIENT border style is active. Set to Color.BLUE when setBorder is called.
-
hightlightColor
@Deprecated public int hightlightColor
Deprecated.Fix the typo, use Control.textShadowColor instead.Change to a color value to highlight the button's text. The highlight is made drawing the button in x-1,y-1; x+1,y-1; x-1,y+1; x+1,y+1 positions. Defaults to -1.- Since:
- TotalCross 1.12
-
commonGap
public static int commonGap
Set commonGap to a value to make all further buttons with the same internal gap. Remember to save the current value and restore it when done. Changing this also affects the size of the ScrollBars created. The value is stored locally on the constructor so it works later if the screen is resized.
-
isSticky
public boolean isSticky
Set to true to make this button sticky: it keeps pressed until the next click. Note that this disables autoRepeat.- Since:
- TotalCross 1.25
-
relativeToText
public java.lang.String relativeToText
Used when the textPosition is RIGHT_OF. Specifies the text that will be used as reference to center this button's text with that one. Usually, you find the biggest text of a set of buttons and place it here.- Since:
- TotalCross 1.27
-
underlinedText
public boolean underlinedText
Set to true to put a line under the text. Here's an example:Button btLink; btLink = new Button("(Guide 124...)"); btLink.underlinedText = true; btLink.setBorder(BORDER_NONE); btLink.setForeColor(Color.BLUE); add(btLink,LEFT,TOP,FILL, PREFERRED);- Since:
- TotalCross 2.0
-
imageHeightFactor
public int imageHeightFactor
The height of the image based on the button's height, ranging from 1 to 100. Used when an image is passed as parameter in one of the constructors.
-
fillWhenPressedOnTransparentBackground
public boolean fillWhenPressedOnTransparentBackground
Fills the button when pressed even if transparentBackground is set.
-
-
Constructor Detail
-
Button
public Button(java.lang.String text, Image img, int textPosition, int gap)Creates a button that shows the given text and image.- Parameters:
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 image- Since:
- TotalCross 1.0
-
Button
public Button(java.lang.String text)
Creates a button displaying the given text.
-
Button
public Button(java.lang.String text, byte border)Creates a button displaying the given text and border.
-
Button
public Button(Image img)
Creates a button with the given image. The transparentColor property of the Image must be set before calling this constructor.
-
Button
public Button(Image img, byte border)
Creates a button with the given image and border. The transparentColor property of the Image must be set before calling this constructor.
-
Button
protected Button()
-
-
Method Detail
-
setText
public void setText(java.lang.String text)
Sets the text that is displayed in the button.- Specified by:
setTextin interfaceTextControl
-
setImage
public void setImage(Image img)
Sets the image that is displayed in the button. The transparentColor property of the Image must be set before calling this method.
Note: this method does not resize the button to fit the image.
-
setPressedColor
public void setPressedColor(int newColor)
Sets the color that the button's background will go ('armed color') when the button gets a PENDOWN event. The default is the cursor color for the background. In Android user interface style, using a bright color may result in a white background. Use a darker color in this case.- Parameters:
newColor- New color to set as the background when pressed.- Since:
- SuperWaba 4.21
-
setBorder
public void setBorder(byte border)
Sets the style of the border.
-
getText
public java.lang.String getText()
Gets the text displayed in the button.- Specified by:
getTextin interfaceTextControl
-
getPreferredWidth
public int getPreferredWidth()
Returns the preffered width of this control.- Overrides:
getPreferredWidthin classControl
-
getPreferredHeight
public int getPreferredHeight()
Returns the preffered height of this control.- Overrides:
getPreferredHeightin classControl
-
simulatePress
public void simulatePress()
Press and depress this Button to simulate that the user had clicked on it. Does not generate events. If isSticky is true, inverts the button state.- Since:
- SuperWaba 5.5
- See Also:
isSticky
-
isPressed
public boolean isPressed()
Returns true if the button is pressed or not. Only makes sense ifisStickyis true. You can change the button state programatically using simulatePress.- Since:
- TotalCross 1.25
- See Also:
simulatePress(),isSticky
-
onEvent
public void onEvent(Event event)
Called by the system to pass events to the button.
-
press
public void press(boolean pressed)
Simulate the press or release of this button. Does not generate events.
-
onPaint
public void onPaint(Graphics g)
Called by the system to draw the button. it cuts the text if the button is too small.
-
onFontChanged
protected void onFontChanged()
Description copied from class:ControlCalled after a setFont- Overrides:
onFontChangedin classControl
-
onBoundsChanged
protected void onBoundsChanged(boolean screenChanged)
Description copied from class:ControlCalled after a setRect.- Overrides:
onBoundsChangedin classControl- Parameters:
screenChanged- If the bounds were changed due to a screen change (rotation, collapse)
-
onColorsChanged
protected void onColorsChanged(boolean colorsChanged)
Description copied from class:ControlCalled after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled- Overrides:
onColorsChangedin classControl
-
paintBackground
protected void paintBackground(Graphics g)
Paint button's background.
-
paintText
protected void paintText(Graphics g, int tx, int ty)
Paint button's text.
-
paintImage
protected void paintImage(Graphics g, boolean bkg, int ix, int iy)
-
getImage
public Image getImage()
Returns the image that is assigned to this Button, or null if none.
-
-