Class EditField

All Implemented Interfaces:
Animation, Editable, EditorHost, ActionSource, StyleListener, TextHolder, TextInputClient

public class EditField extends EditorView implements EditorHost, TextHolder, ActionSource

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);
  • 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: 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: 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: the initial text

      • columns: 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: the initial text

      • hint: 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: the initial text

      • hint: the placeholder shown while empty

      • constraint: 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: the initial text

      • hint: the placeholder shown while empty

      • columns: the number of visible columns

      • constraint: 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: the number of visible rows used to compute the preferred height

      • columns: 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: the initial text

      • rows: the number of visible rows

      • columns: 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: the initial text

      • rows: the number of visible rows

      • columns: the number of visible columns

      • constraint: one of the TextArea constraint constants

  • Method Details

    • isTextInputSupported

      public boolean isTextInputSupported()
      Description copied from interface: EditorHost
      True when the platform can bind a low level text input client (see com.codename1.impl.CodenameOneImplementation#isTextInputSupported).
      Specified by:
      isTextInputSupported in interface EditorHost
    • startTextInput

      public Object startTextInput(TextInputClient client, TextInputConfig config)
      Description copied from interface: EditorHost
      Binds the client to the platform text input source and returns an opaque handle.
      Specified by:
      startTextInput in interface EditorHost
    • updateTextInputState

      public void updateTextInputState(Object handle, TextInputState state)
      Description copied from interface: EditorHost
      Pushes the client's editing state down to the bound input source.
      Specified by:
      updateTextInputState in interface EditorHost
    • stopTextInput

      public void stopTextInput(Object handle)
      Description copied from interface: EditorHost
      Unbinds a previously bound text input client.
      Specified by:
      stopTextInput in interface EditorHost
    • editorChanged

      public void editorChanged()
      Description copied from interface: EditorHost
      Notifies the owning editor that the document content changed so it can fire its change listeners.
      Specified by:
      editorChanged in interface EditorHost
    • fireEditorEvent

      public void fireEditorEvent(String type, String value)
      Description copied from interface: EditorHost
      Delivers a semantic editor event (e.g. a code completion request) back to the owning editor.
      Specified by:
      fireEditorEvent in interface EditorHost
    • getConfig

      public TextInputConfig getConfig()
      Description copied from interface: TextInputClient
      Returns the configuration describing the desired keyboard type and input behavior.
      Specified by:
      getConfig in interface TextInputClient
      Overrides:
      getConfig in class EditorView
    • onEditorAction

      public void onEditorAction(int action)
      Description copied from interface: TextInputClient

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

      Parameters
      • action: one of the TextInputConfig ACTION_* constants
      Specified by:
      onEditorAction in interface TextInputClient
      Overrides:
      onEditorAction in class EditorView
    • handleTypedText

      protected boolean handleTypedText(String text)
      Description copied from class: EditorView

      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: the text being typed / committed
      Overrides:
      handleTypedText in class EditorView
    • setHint

      public EditField setHint(String hint)

      Sets the placeholder text shown while the field is empty.

      Parameters
      • hint: 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: 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: 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: true to allow editing
    • isEditable

      public boolean isEditable()
      Returns whether the field is editable.
      Specified by:
      isEditable in interface Editable
      Overrides:
      isEditable in class Component
    • 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.
      Specified by:
      addActionListener in interface ActionSource
    • removeActionListener

      public void removeActionListener(ActionListener l)
      Removes an action listener.
      Specified by:
      removeActionListener in interface ActionSource
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      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: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class EditorView
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      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

      Overrides:
      calcPreferredSize in class EditorView