public class TextComponent

  1. Object
  2. Component
  3. Container
  4. InputComponent
  5. TextComponent

ImplementsAnimation, Editable, Iterable<Component>, StyleListener

Known subtypesAutoCompleteTextComponent, TextComponentPassword

Encapsulates a text field and label into a single component. This allows the UI to adapt for iOS/Android behavior differences and support features like floating hint when necessary. It also includes platform specific error handling logic.

It is highly recommended to use text components in the context of a com.codename1.ui.layouts.TextModeLayout This allows the layout to implicitly adapt to the on-top mode and use a box layout Y mode for iOS and other platforms.

This class supports several theme constants:

  • textComponentErrorColor a hex RGB color which defaults to null in which case this has no effect. When defined this will change the color of the border and label to the given color to match the material design styling.

  • textComponentErrorLineBorderBool when set to false, this will prevent the text component from applying an underline border when there is a validation error. Defaults to true.

  • textComponentOnTopBool toggles the on top mode see #onTopMode(boolean)

  • textComponentAnimBool toggles the animation mode see #focusAnimation(boolean)

  • textComponentFieldUIID sets the UIID of the text field to something other than TextField which is useful for platforms such as iOS where the look of the text field is different within the text component

The following code demonstrates a simple set of inputs and validation as it appears in iOS, Android and with validation errors

TextModeLayout tl = new TextModeLayout(3, 2);
Form f = new Form("Pixel Perfect", tl);

TextComponent title = new TextComponent().label("Title");
TextComponent price = new TextComponent().label("Price");
TextComponent location = new TextComponent().label("Location");
PickerComponent date = PickerComponent.createDate(new Date()).label("Date");
TextComponent description = new TextComponent().label("Description").multiline(true);

Validator val = new Validator();
val.addConstraint(title, new LengthConstraint(2));
val.addConstraint(price, new NumericConstraint(true));

f.add(tl.createConstraint().widthPercentage(60), title);
f.add(tl.createConstraint().widthPercentage(40), date);
f.add(location);
f.add(price);
f.add(tl.createConstraint().horizontalSpan(2), description);
f.setEditOnShow(title.getField());

f.show();

Constructors

public TextComponent()Default constructor allows us to create an arbitrary text component

Methods

protected void deinitialize()Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy.
public Component getEditor()Returns the editor component e.g. text field picker etc.
public boolean isFocusAnimation()The focus animation mode forces the hint and text to be identical and animates the hint to the label when focus is in the text field as is common on Android.
public TextComponent focusAnimation(boolean focusAnimation)The focus animation mode forces the hint and text to be identical and animates the hint to the label when focus is in the text field as is common on Android.
public TextComponent text(String text)Sets the text of the field
public TextComponent onTopMode(boolean onTopMode)Overridden for covariant return type Sets the on top mode which places the label above the text when true.
public TextComponent action(char icon)Overridden for covariant return type Sets the icon for the action button
public TextComponent actionClick(ActionListener c)Overridden for covariant return type Binds an event for the action button
public TextComponent errorMessage(String errorMessage)Overridden for covariant return type Sets the text of the error label
public TextComponent descriptionMessage(String descriptionMessage)Overridden for covariant return type Sets the text of the description label which currently only applies in the onTop mode.
public TextComponent label(String text)Overridden for covariant return type Sets the text of the label
public TextComponent actionAsButton(boolean asButton)Overridden for covariant return type Indicates the action should behave as a button next to the component and not layered on top of the text component.
public TextComponent actionUIID(String uiid)Overridden for covariant return type Sets the UIID for the action button
public TextComponent actionText(String text)Overridden for covariant return type Provides the text of the action button
public TextComponent labelAndHint(String text)Convenience method for setting the label and hint together
public TextComponent hint(String hint)Sets the hint of the field
public TextComponent hint(Image hint)Sets the hint of the field
public TextComponent multiline(boolean multiline)Sets the text field to multiline or single line
public TextComponent columns(int columns)Sets the columns in the text field
public TextComponent rows(int rows)Sets the rows in the text field
public TextComponent constraint(int constraint)Sets the constraint for text input matching the constraints from the text area class
public TextField getField()Allows us to invoke setters/getters and bind listeners to the text field
public String[] getPropertyNames()A component may expose mutable property names for a UI designer to manipulate, this API is designed for usage internally by the GUI builder code
public Class[] getPropertyTypes()Matches the property names method (see that method for further details).
public String[] getPropertyTypeNames()This method is here to workaround an XMLVM array type bug where property types aren’t identified properly, it returns the names of the types using the following type names: String,int,double,long,byte,short,char,String[],String[][],byte[],I…
public Object getPropertyValue(String name)Returns the current value of the property name, this method is used by the GUI builder
public String setPropertyValue(String name, Object value)Sets a new value to the given property, returns an error message if failed and null if successful.
public String getText()Returns the text in the field com.codename1.ui.TextArea#getText()

Inherited fields

Inherited methods

From Container

encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, layoutContainer, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, pointerPressed, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, 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, getBorder, getScrollable, paintBackground, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, 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, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, laidOut, initComponent, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, 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, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip

Constructor details

TextComponent

public TextComponent()
Default constructor allows us to create an arbitrary text component

Method details

deinitialize

protected void deinitialize()
Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.

getEditor

public Component getEditor()
Returns the editor component e.g. text field picker etc.

Returns

the editor component

isFocusAnimation

public boolean isFocusAnimation()
The focus animation mode forces the hint and text to be identical and animates the hint to the label when focus is in the text field as is common on Android. This can be customized using the theme constant textComponentAnimBool which is true by default on Android. Notice that this is designed for the onTopMode and might not work if that is set to false…

Returns

true if the text should be on top

focusAnimation

public TextComponent focusAnimation(boolean focusAnimation)
The focus animation mode forces the hint and text to be identical and animates the hint to the label when focus is in the text field as is common on Android. This can be customized using the theme constant textComponentAnimBool which is true by default on Android. Notice that this is designed for the onTopMode and might not work if that is set to false…

Parameters

focusAnimation boolean
true for the label to animate into place on focus, false otherwise

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

text

public TextComponent text(String text)
Sets the text of the field

Parameters

text String
the text

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

onTopMode

public TextComponent onTopMode(boolean onTopMode)
Overridden for covariant return type Sets the on top mode which places the label above the text when true. It’s to the left of the text otherwise (right in bidi languages). This is determined by the platform theme using the textComponentOnTopBool theme constant which defaults to false

Parameters

onTopMode boolean
true for the label to be above the text

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

action

public TextComponent action(char icon)
Overridden for covariant return type Sets the icon for the action button

Parameters

icon char
the icon constant from com.codename1.ui.FontImage

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

actionClick

public TextComponent actionClick(ActionListener c)
Overridden for covariant return type Binds an event for the action button

Parameters

c ActionListener
action listener callback

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

errorMessage

public TextComponent errorMessage(String errorMessage)
Overridden for covariant return type Sets the text of the error label

Parameters

errorMessage String
the text

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

descriptionMessage

public TextComponent descriptionMessage(String descriptionMessage)
Overridden for covariant return type Sets the text of the description label which currently only applies in the onTop mode. This text occupies the same space as the error message and thus hides when there’s an error

Parameters

descriptionMessage String
the text

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

label

public TextComponent label(String text)
Overridden for covariant return type Sets the text of the label

Parameters

text String
the text

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

actionAsButton

public TextComponent actionAsButton(boolean asButton)
Overridden for covariant return type Indicates the action should behave as a button next to the component and not layered on top of the text component. This is useful for UI in the style of a browse button next to a text field.

Parameters

asButton boolean
true so the action will act like a button

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

actionUIID

public TextComponent actionUIID(String uiid)
Overridden for covariant return type Sets the UIID for the action button

Parameters

uiid String
a custom UIID for the action

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

actionText

public TextComponent actionText(String text)
Overridden for covariant return type Provides the text of the action button

Parameters

text String
the text that should appear on the action button

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

labelAndHint

public TextComponent labelAndHint(String text)
Convenience method for setting the label and hint together

Parameters

text String
the text and hint

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

hint

public TextComponent hint(String hint)
Sets the hint of the field

Parameters

hint String
the text of the hint

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

hint

public TextComponent hint(Image hint)
Sets the hint of the field

Parameters

hint Image
the icon for the hint

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

multiline

public TextComponent multiline(boolean multiline)
Sets the text field to multiline or single line

Parameters

multiline boolean
true for multiline, false otherwise

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

columns

public TextComponent columns(int columns)
Sets the columns in the text field

Parameters

columns int
the number of columns which is used for preferred size calculations

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

rows

public TextComponent rows(int rows)
Sets the rows in the text field

Parameters

rows int
the number of rows which is used for preferred size calculations

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

constraint

public TextComponent constraint(int constraint)
Sets the constraint for text input matching the constraints from the text area class

Parameters

constraint int
one of the constants from the com.codename1.ui.TextArea class see com.codename1.ui.TextArea#setConstraint(int)

Returns

this for chaining calls E.g. TextComponent tc = new TextComponent().text("Text").label("Label");

getField

public TextField getField()
Allows us to invoke setters/getters and bind listeners to the text field

Returns

the text field instance

getPropertyNames

public String[] getPropertyNames()
A component may expose mutable property names for a UI designer to manipulate, this API is designed for usage internally by the GUI builder code

Returns

the property names allowing mutation

getPropertyTypes

public Class[] getPropertyTypes()
Matches the property names method (see that method for further details).

Returns

the types of the properties

getPropertyTypeNames

public String[] getPropertyTypeNames()
This method is here to workaround an XMLVM array type bug where property types aren’t identified properly, it returns the names of the types using the following type names: String,int,double,long,byte,short,char,String[],String[][],byte[],Image,Image[],Object[],ListModel,ListCellRenderer

Returns

Array of type names

getPropertyValue

public Object getPropertyValue(String name)
Returns the current value of the property name, this method is used by the GUI builder

Parameters

name String
the name of the property

Returns

the value of said property

setPropertyValue

public String setPropertyValue(String name, Object value)
Sets a new value to the given property, returns an error message if failed and null if successful. Notice that some builtin properties such as “$designMode” might be sent to components to indicate application state.

Parameters

name String
the name of the property
value Object
new value for the property

Returns

error message or null

getText

public String getText()
Returns the text in the field com.codename1.ui.TextArea#getText()

Returns

the text