public class EditField

  1. Object
  2. Component
  3. EditorView
  4. EditField

ImplementsActionSource, Animation, Editable, EditorHost, StyleListener, TextHolder, TextInputClient

A pure Codename One text field.

EditField is a drop-in alternative to TextField / TextArea that edits text entirely with the framework’s own text engine (the same engine that backs CodeEditor and RichTextArea) instead of a native peer. Selection, caret movement, word / line navigation, undo and clipboard all work on every port: on iOS, Android and JavaScript the platform soft keyboard, hardware keyboard and IME drive it through the low-level text-input SPI; on the desktop ports the raw physical-keyboard path is used. Because there is no native peer the field is a real, self-contained lightweight component – it never floats above the UI, scrolls perfectly with its parent and can live anywhere a Component can.

It intentionally mirrors the common TextField / TextArea API (#setText(String), #getText(), #setHint(String), #setConstraint(int), #addDataChangedListener(DataChangedListener), #addActionListener(ActionListener), #setSingleLineTextArea(boolean)) and implements the same TextHolder and ActionSource interfaces those components do, so code written against those interfaces (validators, form binding, generic listeners) works unchanged. It also offers the same constructor families: the TextField-style constructors create single-line fields while the TextArea-style (rows, columns) constructors create multi-line editors, making the single- vs multi-line choice explicit at construction. It is still a distinct class – not assignable to TextField / TextArea – so switching to the pure engine is always a deliberate, opt-in choice.

// single line, like TextField
EditField name = new EditField("", "Name", TextArea.EMAILADDR);
name.addActionListener(e -> submit(name.getText()));
form.add(name);

// multi line, like TextArea
EditField notes = new EditField(5, 30);
form.add(notes);

Constructors

public EditField()Creates an empty single-line field.
public EditField(int columns)Creates an empty single-line field with the given visible column count.
public EditField(String text)Creates a single-line field with the given initial text.
public EditField(String text, int columns)Creates a single-line field with the given text and visible column count.
public EditField(String text, String hint)Creates a single-line field with the given text and hint.
public EditField(String text, String hint, int constraint)Creates a single-line field with the given text, hint and constraint.
public EditField(String text, String hint, int columns, int constraint)Creates a single-line field with the given text, hint, visible column count and constraint.
public EditField(int rows, int columns)Creates an empty multi-line field with the given visible row and column counts (the TextArea equivalent).
public EditField(String text, int rows, int columns)Creates a multi-line field with the given text, visible row and column counts (the TextArea equivalent).
public EditField(String text, int rows, int columns, int constraint)Creates a multi-line field with the given text, visible row and column counts and constraint (the TextArea equivalent).

Methods

public boolean isTextInputSupported()True when the platform can bind a low level text input client (see com.codename1.impl.CodenameOneImplementation#isTextInputSupported).
public Object startTextInput(TextInputClient client, TextInputConfig config)Binds the client to the platform text input source and returns an opaque handle.
public void updateTextInputState(Object handle, TextInputState state)Pushes the client’s editing state down to the bound input source.
public void stopTextInput(Object handle)Unbinds a previously bound text input client.
public void editorChanged()Notifies the owning editor that the document content changed so it can fire its change listeners.
public void fireEditorEvent(String type, String value)Delivers a semantic editor event (e.g. a code completion request) back to the owning editor.
public TextInputConfig getConfig()Returns the configuration describing the desired keyboard type and input behavior.
public void onEditorAction(int action)Delivers the keyboard return key action (done / next / search / send) configured through TextInputConfig.
protected boolean handleTypedText(String text)Hook giving feature layers a chance to intercept typed text before the default insertion (used by the code editor for bracket / quote auto close, tab expansion and auto indent).
public EditField setHint(String hint)Sets the placeholder text shown while the field is empty.
public String getHint()Returns the placeholder text.
public boolean isSingleLineTextArea()True for a single-line field (the default).
public void setSingleLineTextArea(boolean singleLine)Sets whether this is a single-line field.
public void setConstraint(int constraint)Sets the input constraint (one of the TextArea constraint constants) that hints the platform keyboard (e.g. email, numeric, URL).
public int getConstraint()Returns the input constraint.
public void setActionType(int actionType)Sets the soft-keyboard action button for a single-line field (one of the TextInputConfig#ACTION_DONE family).
public void setRows(int rows)Sets the number of visible rows used to compute the preferred height (multi-line fields).
public int getRows()Returns the number of visible rows.
public void setColumns(int columns)Sets the number of visible columns used to compute the preferred width.
public int getColumns()Returns the number of visible columns.
public void setEditable(boolean editable)Sets whether the field is editable.
public boolean isEditable()Returns whether the field is editable.
public void addDataChangedListener(DataChangedListener l)Adds a listener notified whenever the text changes.
public void removeDataChangedListener(DataChangedListener l)Removes a data changed listener.
public void addActionListener(ActionListener l)Adds a listener notified when the action key (return on a single-line field) is pressed.
public void removeActionListener(ActionListener l)Removes an action listener.
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.
protected Dimension calcPreferredSize()Calculates the preferred size based on component content.

Inherited fields

Inherited methods

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, initLaf, 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, 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, fireClicked, isSelectableInteraction, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, 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, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, setShouldCalcPreferredSize, handlesInput, setHandlesInput, 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, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, dragInitiated, drawDraggedImage, draggingOver, dragEnter, dragExit, drop, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, isStickyDrag, pointerPressed, isDragAndDropOperation, 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, paramString, refreshTheme, refreshTheme, refreshTheme, isDragActivated, getGridPosY, getGridPosX, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, setCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, initComponent, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, isEnabled, setEnabled, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, 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, 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

EditField

public EditField()
Creates an empty single-line field.

EditField

public EditField(int columns)
Creates an empty single-line field with the given visible column count.

Parameters

columns int
the number of visible columns used to compute the preferred width

EditField

public EditField(String text)
Creates a single-line field with the given initial text.

Parameters

text String
the initial text

EditField

public EditField(String text, int columns)
Creates a single-line field with the given text and visible column count.

Parameters

text String
the initial text
columns int
the number of visible columns

EditField

public EditField(String text, String hint)
Creates a single-line field with the given text and hint.

Parameters

text String
the initial text
hint String
the placeholder shown while empty

EditField

public EditField(String text, String hint, int constraint)
Creates a single-line field with the given text, hint and constraint.

Parameters

text String
the initial text
hint String
the placeholder shown while empty
constraint int
one of the TextArea constraint constants (e.g. TextArea#EMAILADDR)

EditField

public EditField(String text, String hint, int columns, int constraint)
Creates a single-line field with the given text, hint, visible column count and constraint.

Parameters

text String
the initial text
hint String
the placeholder shown while empty
columns int
the number of visible columns
constraint int
one of the TextArea constraint constants (e.g. TextArea#EMAILADDR)

EditField

public EditField(int rows, int columns)
Creates an empty multi-line field with the given visible row and column counts (the TextArea equivalent). Using this constructor sets the field to multi-line mode.

Parameters

rows int
the number of visible rows used to compute the preferred height
columns int
the number of visible columns

EditField

public EditField(String text, int rows, int columns)
Creates a multi-line field with the given text, visible row and column counts (the TextArea equivalent). Using this constructor sets the field to multi-line mode.

Parameters

text String
the initial text
rows int
the number of visible rows
columns int
the number of visible columns

EditField

public EditField(String text, int rows, int columns, int constraint)
Creates a multi-line field with the given text, visible row and column counts and constraint (the TextArea equivalent). Using this constructor sets the field to multi-line mode.

Parameters

text String
the initial text
rows int
the number of visible rows
columns int
the number of visible columns
constraint int
one of the TextArea constraint constants

Method details

isTextInputSupported

public boolean isTextInputSupported()
True when the platform can bind a low level text input client (see com.codename1.impl.CodenameOneImplementation#isTextInputSupported).

startTextInput

public Object startTextInput(TextInputClient client, TextInputConfig config)
Binds the client to the platform text input source and returns an opaque handle.

updateTextInputState

public void updateTextInputState(Object handle, TextInputState state)
Pushes the client’s editing state down to the bound input source.

stopTextInput

public void stopTextInput(Object handle)
Unbinds a previously bound text input client.

editorChanged

public void editorChanged()
Notifies the owning editor that the document content changed so it can fire its change listeners.

fireEditorEvent

public void fireEditorEvent(String type, String value)
Delivers a semantic editor event (e.g. a code completion request) back to the owning editor.

getConfig

public TextInputConfig getConfig()
Returns the configuration describing the desired keyboard type and input behavior.

onEditorAction

public void onEditorAction(int action)
Delivers the keyboard return key action (done / next / search / send) configured through TextInputConfig.

Parameters

action int
one of the TextInputConfig ACTION_* constants

handleTypedText

protected boolean handleTypedText(String text)
Hook giving feature layers a chance to intercept typed text before the default insertion (used by the code editor for bracket / quote auto close, tab expansion and auto indent). Return true when the text was consumed. The default returns false.

Parameters

text String
the text being typed / committed

setHint

public EditField setHint(String hint)
Sets the placeholder text shown while the field is empty.

Parameters

hint String
the placeholder, or null / empty for none

getHint

public String getHint()
Returns the placeholder text.

isSingleLineTextArea

public boolean isSingleLineTextArea()
True for a single-line field (the default). A single-line field fires its action listeners on the return key instead of inserting a line break.

setSingleLineTextArea

public void setSingleLineTextArea(boolean singleLine)
Sets whether this is a single-line field. Passing false makes it a multi-line editor.

Parameters

singleLine boolean
true for a single line, false for multi-line

setConstraint

public void setConstraint(int constraint)
Sets the input constraint (one of the TextArea constraint constants) that hints the platform keyboard (e.g. email, numeric, URL).

Parameters

constraint int
a TextArea constraint constant

getConstraint

public int getConstraint()
Returns the input constraint.

setActionType

public void setActionType(int actionType)
Sets the soft-keyboard action button for a single-line field (one of the TextInputConfig#ACTION_DONE family).

setRows

public void setRows(int rows)
Sets the number of visible rows used to compute the preferred height (multi-line fields).

getRows

public int getRows()
Returns the number of visible rows.

setColumns

public void setColumns(int columns)
Sets the number of visible columns used to compute the preferred width.

getColumns

public int getColumns()
Returns the number of visible columns.

setEditable

public void setEditable(boolean editable)
Sets whether the field is editable. A non-editable field still allows selection and copying.

Parameters

editable boolean
true to allow editing

isEditable

public boolean isEditable()
Returns whether the field is editable.

addDataChangedListener

public void addDataChangedListener(DataChangedListener l)
Adds a listener notified whenever the text changes.

removeDataChangedListener

public void removeDataChangedListener(DataChangedListener l)
Removes a data changed listener.

addActionListener

public void addActionListener(ActionListener l)
Adds a listener notified when the action key (return on a single-line field) is pressed.

removeActionListener

public void removeActionListener(ActionListener l)
Removes an action listener.

paint

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.

Parameters

g Graphics
the component graphics

calcPreferredSize

protected Dimension calcPreferredSize()
Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

Returns

the calculated preferred size based on component content