Class TextField

All Implemented Interfaces:
Styleable, EventTarget, Skinnable
Direct Known Subclasses:
PasswordField

public class TextField extends TextInputControl
Text input component that allows a user to enter a single line of unformatted text. Unlike in previous releases of JavaFX, support for multi-line input is not available as part of the TextField control, however this is the sole-purpose of the TextArea control. Additionally, if you want a form of rich-text editing, there is also the HTMLEditor control.

TextField supports the notion of showing prompt text to the user when there is no text already in the TextField (either via the user, or set programmatically). This is a useful way of informing the user as to what is expected in the text field, without having to resort to tooltips or on-screen labels.

Example:

 var textField = new TextField("Hello World!");
Image of the TextField control
Since:
JavaFX 2.0
See Also:
  • Property Details

  • Field Details

  • Constructor Details

    • TextField

      public TextField()
      Creates a TextField with empty text content.
    • TextField

      public TextField(String text)
      Creates a TextField with initial text content.
      Parameters:
      text - A string for text content.
  • Method Details

    • getCharacters

      public CharSequence getCharacters()
      Returns the character sequence backing the text field's content.
      Returns:
      the character sequence backing the text field's content
    • prefColumnCountProperty

      public final IntegerProperty prefColumnCountProperty()
      The preferred number of text columns. This is used for calculating the TextField's preferred width.
      Returns:
      the prefColumnCount property
      See Also:
    • getPrefColumnCount

      public final int getPrefColumnCount()
      Gets the value of the prefColumnCount property.
      Property description:
      The preferred number of text columns. This is used for calculating the TextField's preferred width.
      Returns:
      the value of the prefColumnCount property
      See Also:
    • setPrefColumnCount

      public final void setPrefColumnCount(int value)
      Sets the value of the prefColumnCount property.
      Property description:
      The preferred number of text columns. This is used for calculating the TextField's preferred width.
      Parameters:
      value - the value for the prefColumnCount property
      See Also:
    • onActionProperty

      public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty()
      The action handler associated with this text field, or null if no action handler is assigned. The action handler is normally called when the user types the ENTER key.
      Returns:
      the onAction property
      See Also:
    • getOnAction

      public final EventHandler<ActionEvent> getOnAction()
      Gets the value of the onAction property.
      Property description:
      The action handler associated with this text field, or null if no action handler is assigned. The action handler is normally called when the user types the ENTER key.
      Returns:
      the value of the onAction property
      See Also:
    • setOnAction

      public final void setOnAction(EventHandler<ActionEvent> value)
      Sets the value of the onAction property.
      Property description:
      The action handler associated with this text field, or null if no action handler is assigned. The action handler is normally called when the user types the ENTER key.
      Parameters:
      value - the value for the onAction property
      See Also:
    • alignmentProperty

      public final ObjectProperty<Pos> alignmentProperty()
      Specifies how the text should be aligned when there is empty space within the TextField.
      Returns:
      the alignment property
      Since:
      JavaFX 2.1
      See Also:
    • setAlignment

      public final void setAlignment(Pos value)
      Sets the value of the alignment property.
      Property description:
      Specifies how the text should be aligned when there is empty space within the TextField.
      Parameters:
      value - the value for the alignment property
      Since:
      JavaFX 2.1
      See Also:
    • getAlignment

      public final Pos getAlignment()
      Gets the value of the alignment property.
      Property description:
      Specifies how the text should be aligned when there is empty space within the TextField.
      Returns:
      the value of the alignment property
      Since:
      JavaFX 2.1
      See Also:
    • getClassCssMetaData

      public static List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
      Gets the CssMetaData associated with this class, which may include the CssMetaData of its superclasses.
      Returns:
      the CssMetaData
      Since:
      JavaFX 8.0
    • getControlCssMetaData

      public List<CssMetaData<? extends Styleable,?>> getControlCssMetaData()
      Gets the unmodifiable list of the control's CSS-styleable properties.
      Overrides:
      getControlCssMetaData in class TextInputControl
      Returns:
      the unmodifiable list of the control's CSS-styleable properties
      Since:
      JavaFX 8.0