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:
    TextArea
    • Constructor Detail

      • 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 Detail

      • getCharacters

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

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

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

        public final EventHandler<ActionEvent> getOnAction()
        Gets the value of the property onAction.
        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.
      • setOnAction

        public final void setOnAction​(EventHandler<ActionEvent> value)
        Sets the value of the property onAction.
        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.
      • setAlignment

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

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

        protected Skin<?> createDefaultSkin()
        Create a new instance of the default skin for this control. This is called to create a skin for the control if no skin is provided via CSS -fx-skin or set explicitly in a sub-class with setSkin(...).
        Overrides:
        createDefaultSkin in class Control
        Returns:
        new instance of default skin for this control. If null then the control will have no skin unless one is provided by css.
      • getClassCssMetaData

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