Class TextField

All Implemented Interfaces:
Animation, Editable, ActionSource, StyleListener, TextHolder
Direct Known Subclasses:
AutoCompleteTextField

public class TextField extends TextArea

A specialized version of com.codename1.ui.TextArea with some minor deviations from the original specifically:

  • Blinking cursor is rendered on TextField only

  • com.codename1.ui.events.DataChangeListener is only available in TextField. This is crucial for character by character input event tracking

  • com.codename1.ui.TextField#setDoneListener(com.codename1.ui.events.ActionListener) is only available in TextField

  • Different UIID's ("TextField" vs. "TextArea")

The demo code below shows simple input using text fields:

TableLayout tl;
int spanButton = 2;
if(Display.getInstance().isTablet()) {
    tl = new TableLayout(7, 2);
} else {
    tl = new TableLayout(14, 1);
    spanButton = 1;
}
tl.setGrowHorizontally(true);
hi.setLayout(tl);

TextField firstName = new TextField("", "First Name", 20, TextArea.ANY);
TextField surname = new TextField("", "Surname", 20, TextArea.ANY);
TextField email = new TextField("", "E-Mail", 20, TextArea.EMAILADDR);
TextField url = new TextField("", "URL", 20, TextArea.URL);
TextField phone = new TextField("", "Phone", 20, TextArea.PHONENUMBER);

TextField num1 = new TextField("", "1234", 4, TextArea.NUMERIC);
TextField num2 = new TextField("", "1234", 4, TextArea.NUMERIC);
TextField num3 = new TextField("", "1234", 4, TextArea.NUMERIC);
TextField num4 = new TextField("", "1234", 4, TextArea.NUMERIC);

Button submit = new Button("Submit");
TableLayout.Constraint cn = tl.createConstraint();
cn.setHorizontalSpan(spanButton);
cn.setHorizontalAlign(Component.RIGHT);
hi.add("First Name").add(firstName).
        add("Surname").add(surname).
        add("E-Mail").add(email).
        add("URL").add(url).
        add("Phone").add(phone).
        add("Credit Card").
                add(GridLayout.encloseIn(4, num1, num2, num3, num4)).
        add(cn, submit);

The following code demonstrates a more advanced search widget where the data is narrowed as we type directly into the title area search. Notice that the TextField and its hint are styled to look like the title.

Toolbar.setGlobalToolbar(true);
Style s = UIManager.getInstance().getComponentStyle("Title");

Form hi = new Form("Toolbar", new BoxLayout(BoxLayout.Y_AXIS));
TextField searchField = new TextField("", "Toolbar Search"); //
searchField.getHintLabel().setUIID("Title");
searchField.setUIID("Title");
searchField.getAllStyles().setAlignment(Component.LEFT);
hi.getToolbar().setTitleComponent(searchField);
FontImage searchIcon = FontImage.createMaterial(FontImage.MATERIAL_SEARCH, s);
searchField.addDataChangeListener((i1, i2) -> { //
    String t = searchField.getText();
    if(t.length()  -1;
            cmp.setHidden(!show); //
            cmp.setVisible(show);
        }
    }
    hi.getContentPane().animateLayout(250);
});
hi.getToolbar().addCommandToRightBar("", searchIcon, (e) -> {
    searchField.startEditingAsync(); //
});

hi.add("A Game of Thrones").
        add("A Clash Of Kings").
        add("A Storm Of Swords").
        add("A Feast For Crows").
        add("A Dance With Dragons").
        add("The Winds of Winter").
        add("A Dream of Spring");
hi.show();
  • Constructor Details

    • TextField

      public TextField()
      Default constructor
    • TextField

      public TextField(int columns)

      Construct a text field with space reserved for columns

      Parameters
      • columns: - the number of columns
    • TextField

      public TextField(String text)

      Construct text field

      Parameters
      • text: the text of the field
    • TextField

      public TextField(String text, String hint)

      Construct text field with a hint

      Parameters
      • text: the text of the field

      • hint: the hint string

    • TextField

      public TextField(String text, String hint, int columns, int constraint)

      Construct text field with a hint, columns and constraint values

      Parameters
      • text: the text of the field

      • hint: the hint string

      • columns: columns value

      • constraint: the constraint value

    • TextField

      public TextField(String text, int columns)

      Construct text field

      Parameters
      • text: the text of the field

      • columns: - the number of columns

  • Method Details

    • isUseNativeTextInput

      public static boolean isUseNativeTextInput()

      Indicates that native text input should be used in text field when in place editing is supported by the platform

      Returns

      the useNativeTextInput

      Deprecated

      this API is no longer useful and should be avoided

    • setUseNativeTextInput

      public static void setUseNativeTextInput(boolean aUseNativeTextInput)

      Indicates that native text input should be used in text field when in place editing is supported by the platform

      Parameters
      • aUseNativeTextInput: the useNativeTextInput to set
      Deprecated

      this API is no longer useful and should be avoided

    • setClearText

      public static void setClearText(String text)

      Set the text that should appear on the clear softkey

      Parameters
      • text: localized text for the clear softbutton
    • setT9Text

      public static void setT9Text(String text)

      Set the text that should appear on the T9 softkey

      Parameters
      • text: text for the T9 softbutton
    • create

      public static TextArea create(String text, int columns)

      Construct text field/area depending on whether native in place editing is supported

      Parameters
      • text: the text of the field

      • columns: - the number of columns

      Returns

      a text field if native in place editing is unsupported and a text area if it is

    • create

      public static TextArea create()

      Default factory method

      Returns

      a text field if native in place editing is unsupported and a text area if it is

    • create

      public static TextArea create(int columns)

      Construct text field/area depending on whether native in place editing is supported

      Parameters
      • columns: - the number of columns
      Returns

      a text field if native in place editing is unsupported and a text area if it is

    • create

      public static TextArea create(String text)

      Construct text field/area depending on whether native in place editing is supported

      Parameters
      • text: the text of the field
      Returns

      a text field if native in place editing is unsupported and a text area if it is

    • addInputMode

      public static void addInputMode(String name, Hashtable values, boolean firstUpcase)

      Adds a new inputmode hashtable with the given name and set of values

      Parameters
      • name: a unique display name for the input mode e.g. ABC, 123 etc...

      • values: @param values The key for the hashtable is an Integer keyCode and the value is a String containing the characters to toggle between for the given keycode

      • firstUpcase: @param firstUpcase indicates if this input mode in an input mode used for the special case where the first letter is an upper case letter

      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • getDefaultInputModeOrder

      public static String[] getDefaultInputModeOrder()

      Returns the order in which input modes are toggled by default

      Returns

      the default order of the input mode

      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • setDefaultInputModeOrder

      public static void setDefaultInputModeOrder(String[] order)

      Sets the order in which input modes are toggled by default and allows disabling/hiding an input mode

      Parameters
      • order: the order for the input modes in all future created fields
      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • getSymbolTable

      public static char[] getSymbolTable()

      Returns the symbol table for the device

      Returns

      the symbol table of the device for the symbol table input

    • setSymbolTable

      public static void setSymbolTable(char[] table)

      Sets the symbol table to show when the user clicks the symbol table key

      Parameters
      • table: the symbol table of the device for the symbol table input
    • isReplaceMenuDefault

      public static boolean isReplaceMenuDefault()

      Indicates whether the menu of the form should be replaced with the T9/Clear commands for the duration of interactivity with the text field

      Returns

      true if the menu should be replaced

    • setReplaceMenuDefault

      public static void setReplaceMenuDefault(boolean replaceMenu)

      Indicates whether the menu of the form should be replaced with the T9/Clear commands for the duration of interactivity with the text field

      Parameters
      • replaceMenu: true if the menu should be replaced
    • isQwertyAutoDetect

      public static boolean isQwertyAutoDetect()

      Indicates whether the text field should try to auto detect qwerty and switch the qwerty device flag implicitly

      Returns

      true for qwerty auto detection

    • setQwertyAutoDetect

      public static void setQwertyAutoDetect(boolean v)

      Indicates whether the text field should try to auto detect qwerty and switch the qwerty device flag implicitly

      Parameters
      • v: true for qwerty auto detection
    • isQwertyDevice

      public static boolean isQwertyDevice()

      The default value for the qwerty flag so it doesn't need setting for every text field individually.

      Returns

      true for qwerty devices

    • setQwertyDevice

      public static void setQwertyDevice(boolean v)

      The default value for the qwerty flag so it doesn't need setting for every text field individually.

      Parameters
      • v: true for qwerty device
    • getDefaultChangeInputModeKey

      public static int getDefaultChangeInputModeKey()

      Key to change the input mode on the device

      Returns

      key to change the input mode

      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • setDefaultChangeInputModeKey

      public static void setDefaultChangeInputModeKey(int k)

      Key to change the input mode on the device

      Parameters
      • k: key to change the input mode
      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • getDefaultSymbolDialogKey

      public static int getDefaultSymbolDialogKey()

      The default key for poping open the symbol dialog

      Returns

      the default key

    • setDefaultSymbolDialogKey

      public static void setDefaultSymbolDialogKey(int d)

      The default key for poping open the symbol dialog

      Parameters
      • d: new key value
    • isEnableInputScroll

      public boolean isEnableInputScroll()

      Indicates whether text field input should scroll to the right side when no more room for the input is present.

      Returns

      true if scrolling is enabled

      Overrides:
      isEnableInputScroll in class TextArea
    • setEnableInputScroll

      public void setEnableInputScroll(boolean enableInputScroll)

      Indicates whether text field input should scroll to the right side when no more room for the input is present.

      Parameters
      • enableInputScroll: true to enable scrolling to the side
    • deleteChar

      public void deleteChar()
      Performs a backspace operation
    • commitChange

      protected void commitChange()
      Commit the changes made to the text field as a complete edit operation. This is used in a numeric keypad to allow the user to repeatedly press a number to change values.
    • isPendingCommit

      public boolean isPendingCommit()

      Returns true if the text field is waiting for a commit on editing

      Returns

      true if a commit is pending

      Overrides:
      isPendingCommit in class TextArea
    • getCommitTimeout

      public int getCommitTimeout()

      The amount of time in milliseconds it will take for a change to get committed into the field.

      Returns

      the time for a commit timeout

    • setCommitTimeout

      public void setCommitTimeout(int commitTimeout)

      The amount of time in milliseconds it will take for a change to get committed into the field.

      Parameters
      • commitTimeout: @param commitTimeout indicates the amount of time that should elapse for a commit to automatically occur
    • getInputMode

      public String getInputMode()

      Returns the currently selected input mode

      Returns
      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

      Overrides:
      getInputMode in class TextArea
      Returns:
      the display name of the input mode by default the following modes are supported: Abc, ABC, abc, 123
    • setInputMode

      public void setInputMode(String inputMode)

      Sets the current selected input mode matching one of the existing input modes

      Parameters
      • inputMode: @param inputMode the display name of the input mode by default the following modes are supported: Abc, ABC, abc, 123
      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • isChangeInputMode

      protected boolean isChangeInputMode(int keyCode)

      Indicates whether the key changes the current input mode

      Parameters
      • keyCode: the code
      Returns

      true for the hash (#) key code

      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • getInputModeOrder

      public String[] getInputModeOrder()

      Returns the order in which input modes are toggled

      Returns

      the order of the input modes

      Overrides:
      getInputModeOrder in class TextArea
    • setInputModeOrder

      public void setInputModeOrder(String[] order)

      Sets the order in which input modes are toggled and allows disabling/hiding an input mode

      Parameters
      • order: the order for the input modes in this field
      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • getLongClickInputMode

      protected String getLongClickInputMode()

      Returns the input mode for the ong click mode

      Returns

      returns 123 by default

    • getCharPerKeyCode

      protected char getCharPerKeyCode(int pressCount, int keyCode, boolean longClick)

      Returns the character matching the given key code after the given amount of user presses

      Parameters
      • pressCount: number of times this keycode was pressed

      • keyCode: the actual keycode input by the user

      • longClick: does this click constitute a long click

      Returns
      Returns:
      the char mapping to this key or 0 if no appropriate char was found (navigation, input mode change etc...).
    • getCursorPosition

      public int getCursorPosition()

      Returns the position of the cursor char position

      Returns

      the cursor position

      Overrides:
      getCursorPosition in class TextArea
    • setCursorPosition

      public void setCursorPosition(int pos)

      Sets the position of the cursor char position

      Parameters
      • pos: the cursor position
    • getCursorY

      public int getCursorY()

      Returns the position of the cursor line position

      Returns

      the cursor line position

      Overrides:
      getCursorY in class TextArea
    • getCursorX

      public int getCursorX()

      Returns the position of the cursor char position in the current line.

      Returns

      the cursor char position in the current line

      Overrides:
      getCursorX in class TextArea
    • setText

      public void setText(String text)

      Sets the text within this text area

      Parameters
      • t: new value for the text area
      Specified by:
      setText in interface TextHolder
      Overrides:
      setText in class TextArea
    • clear

      public void clear()
      Clears the text from the TextField
    • isClearKey

      protected boolean isClearKey(int keyCode)

      Returns true if this is the clear key on the device, many devices don't contain a clear key and even in those that contain it this might be an issue

      Parameters
      • keyCode: the key code that might be the clear key
      Returns

      true if this is the clear key.

    • longKeyPress

      protected void longKeyPress(int keyCode)

      If this Component is focused this method is invoked when the user presses and holds the key

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      longKeyPress in class Component
    • isQwertyInput

      public boolean isQwertyInput()

      True is this is a qwerty device or a device that is currently in qwerty mode.

      Returns

      currently defaults to false

      Overrides:
      isQwertyInput in class TextArea
    • setQwertyInput

      public void setQwertyInput(boolean qwerty)

      True is this is a qwerty device or a device that is currently in qwerty mode.

      Parameters
      • qwerty: the value of qwerty mode
    • isImmediateInputMode

      protected boolean isImmediateInputMode(String mode)

      Returns true if the given input mode should commit immediately or wait for the commit timeout

      Parameters
      • mode: the input mode
      Returns

      returns true for input mode 123 by default

      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • insertChars

      public void insertChars(String c)

      This method is responsible for adding a character into the field and is the focal point for all input. It can be overriden to prevent a particular char from insertion or provide a different behavior for char insertion. It is the responsibility of this method to shift the cursor and invoke setText...

      This method accepts a string for the more elaborate cases such as multi-char input and paste.

      Parameters
      • c: character for insertion
      Deprecated

      this is a method for use only on old J2ME devices and is ignored everywhere else

    • validChar

      public boolean validChar(String c)

      Checks if the candidate input is valid for this TextField

      Parameters
      • c: the String to insert
      Returns

      true if the String is valid

    • showSymbolDialog

      protected void showSymbolDialog()
      Invoked to show the symbol dialog, this method can be overriden by subclasses to manipulate the symbol table
    • createSymbolTable

      protected Container createSymbolTable()

      Creates a symbol table container used by the showSymbolDialog method. This method is designed for subclases to override and customize.

      Returns

      container for the symbol table.

    • keyReleased

      public void keyReleased(int keyCode)

      If this Component is focused, the key released event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyReleased in class TextArea
    • getLongClickDuration

      protected int getLongClickDuration()

      The amount of time considered as a "long click" causing the long click method to be invoked.

      Returns

      currently defaults to 800

    • isCursorPositionCycle

      protected boolean isCursorPositionCycle()

      Returns true if the cursor should cycle to the beginning of the text when the user navigates beyond the edge of the text and visa versa.

      Returns

      true by default

    • isSymbolDialogKey

      protected boolean isSymbolDialogKey(int keyCode)

      Returns true if this keycode is the one mapping to the symbol dialog popup

      Parameters
      • keyCode: the keycode to check
      Returns

      true if this is the star symbol *

    • 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.
      Overrides:
      deinitialize in class TextArea
    • setEditable

      public void setEditable(boolean b)

      Sets this text area to be editable or readonly

      Parameters
      • b: true is text are is editable; otherwise false
      Overrides:
      setEditable in class TextArea
    • keyRepeated

      public void keyRepeated(int keyCode)

      If this Component is focused, the key repeat event will call this method.

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyRepeated in class Component
    • keyPressed

      public void keyPressed(int keyCode)

      If this Component is focused, the key pressed event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyPressed in class TextArea
    • installCommands

      protected Command installCommands(Command clear, Command t9)

      Installs the clear and t9 commands onto the parent form, this method can be overriden to provide device specific placement for these commands

      Parameters
      • clear: the clear command

      • t9: the t9 command

      Returns
      Returns:
      clear command already installed in the form if applicable, none if no clear command was installed before or not applicable.
    • isSelectableInteraction

      protected boolean isSelectableInteraction()

      This method allows a component to indicate that it is interested in an "implicit" select command to appear in the "fire" button when 3 softbuttons are defined in a device.

      Returns

      true if this is a selectable interaction

      Overrides:
      isSelectableInteraction in class TextArea
    • fireClicked

      protected void fireClicked()
      When working in 3 softbutton mode "fire" key (center softbutton) is sent to this method in order to allow 3 button devices to work properly. When overriding this method you should also override isSelectableInteraction to indicate that a command is placed appropriately on top of the fire key for 3 soft button phones.
      Overrides:
      fireClicked in class TextArea
    • removeCommands

      protected void removeCommands(Command clear, Command t9, Command originalClear)

      Removes the clear and t9 commands from the parent form, this method can be overriden to provide device specific placement for these commands

      Parameters
      • clear: the clear command

      • t9: the t9 command

      • originalClear: the command originally assigned as the clear command (or null if no command was assigned before)

    • isEditingTrigger

      protected boolean isEditingTrigger(int keyCode)

      Indicates whether the given key code should be ignored or should trigger editing, by default fire or any numeric key should trigger editing implicitly. This method is only called when handles input is false.

      Parameters
      • keyCode: the keycode passed to the keyPressed method
      Returns

      true if this key code should cause a switch to editing mode.

    • isEditingEndTrigger

      protected boolean isEditingEndTrigger(int keyCode)

      Indicates whether the given key code should be ignored or should trigger cause editing to end. By default the fire key, up or down will trigger the end of editing.

      Parameters
      • keyCode: the keycode passed to the keyPressed method
      Returns

      true if this key code should cause a switch to editing mode.

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

      Overrides:
      calcPreferredSize in class TextArea
    • getCursorBlinkTimeOn

      public int getCursorBlinkTimeOn()

      The amount of time in milliseconds in which the cursor is visible

      Returns

      time for the cursor to stay "on"

    • setCursorBlinkTimeOn

      public void setCursorBlinkTimeOn(int time)

      The amount of time in milliseconds in which the cursor is visible

      Parameters
      • time: for the cursor to stay "on"
    • getCursorBlinkTimeOff

      public int getCursorBlinkTimeOff()

      The amount of time in milliseconds in which the cursor is invisible

      Returns

      time for the cursor to stay "off"

    • setCursorBlinkTimeOff

      public void setCursorBlinkTimeOff(int time)

      The amount of time in milliseconds in which the cursor is invisible

      Parameters
      • time: for the cursor to stay "off"
    • animate

      public boolean animate()

      Allows the animation to reduce "repaint" calls when it returns false. It is called once for every frame. Frames are defined by the com.codename1.ui.Display class.

      Returns

      true if a repaint is desired or false if no repaint is necessary

      Specified by:
      animate in interface Animation
      Overrides:
      animate in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class TextArea
    • isUseSoftkeys

      public boolean isUseSoftkeys()

      When set to true softkeys are used to enable delete functionality

      Returns

      true if softkeys should be used

    • setUseSoftkeys

      public void setUseSoftkeys(boolean useSoftkeys)

      When set to true softkeys are used to enable delete functionality

      Parameters
      • useSoftkeys: true if softkeys should be used
    • isReplaceMenu

      public boolean isReplaceMenu()

      Indicates whether the menu of the form should be replaced with the T9/Clear commands for the duration of interactivity with the text field

      Returns

      true if the menu should be replaced

    • setReplaceMenu

      public void setReplaceMenu(boolean replaceMenu)

      Indicates whether the menu of the form should be replaced with the T9/Clear commands for the duration of interactivity with the text field

      Parameters
      • replaceMenu: true if the menu should be replaced
    • isOverwriteMode

      public boolean isOverwriteMode()

      Indicates that this is the overwrite mode

      Returns

      true if input with overwrite characters

    • setOverwriteMode

      public void setOverwriteMode(boolean overwriteMode)

      Indicates that this is the overwrite mode

      Parameters
      • overwriteMode: set to true if input with overwrite characters
    • isLeftAndRightEditingTrigger

      public boolean isLeftAndRightEditingTrigger()

      Indicates whether the left/right keys will trigger editing, this is true by default. Left and right key edit trigger might be disabled for cases such as text field positioned horizontally one next to the other.

      Returns

      leftAndRightEditingTrigger Indicates whether the left/right keys will trigger editing

    • setLeftAndRightEditingTrigger

      public void setLeftAndRightEditingTrigger(boolean leftAndRightEditingTrigger)

      Indicates whether the left/right keys will trigger editing, this is true by default. Left and right key edit trigger might be disabled for cases such as text field positioned horizontally one next to the other.

      Parameters
      • leftAndRightEditingTrigger: Indicates whether the left/right keys will trigger editing
    • setAlignment

      public void setAlignment(int align)

      Sets the Alignment of the TextArea to one of: CENTER, LEFT, RIGHT

      Parameters
      • align: alignment value
      Deprecated

      use Style.setAlignment instead

      See also
      • #CENTER

      • #LEFT

      • #RIGHT

      Overrides:
      setAlignment in class TextArea
    • calculateTextSelectionSpan

      protected TextSelection.Spans calculateTextSelectionSpan(TextSelection sel)

      Calculates the spans for the the given text selection. This should generally just delegate to the appropriate method in the look and feel for performing the layout calculation.

      Parameters
      • sel: The TextSelection
      Since

      7.0

      Overrides:
      calculateTextSelectionSpan in class TextArea