Skip to content
CrossFly is 100% open sourceFollow us on GitHub
CrossFlyCrossFly
Get started

This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.

Text fields (TextField)

TextField is the single-line field. A fluent API for the essentials; fine-grained behavior comes from the TextFieldStyle.

Complete example (copy, paste and run)

package com.exemplo;

import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.style.*;   // TextFieldStyle (behavioral config)

/** Runnable main screen. Point the launcher (classname) and run: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      // Name field with floating label
      TextField nome = new TextField().caption("Nome");
      nome.<TextFieldStyle>getStyle().floatingCaption(true);
      add(LAY_AUTO, nome);

      // Password field with show/hide button
      TextField senha = new TextField().caption("Senha");
      senha.<TextFieldStyle>getStyle()
         .mode(EditMode.PASSWORD)
         .passwordToggle(true);
      add(LAY_AUTO, senha);

      // Label where the read value will be displayed
      Label resultado = new Label("");
      add(LAY_AUTO, resultado);

      // There is NO per-key callback: read getText() on submit
      Button enviar = new Button("Enviar");
      enviar.onAction(c -> resultado.setText("Nome = " + nome.getText()));
      add(LAY_AUTO, enviar);
   }
}

The snippets below show only the core (what goes inside onShow()). To run them, place them in the MainWindow skeleton above.

Basic usage

TextField nome = new TextField()
   .caption("Nome")                 // floating label
   .text("");                       // initial value
painel.add(LAY_AUTO, nome);

// read the value when needed (there is no per-key event):
Button enviar = new Button("Enviar");
enviar.onAction(c -> Debug.log("Nome = " + nome.getText()));

Constructors and fluent API

Member Effect
new TextField() / new TextField("texto") empty field / with initial value
.caption(String) caption (floats when focused, if floatingCaption)
.text(String) / setText(String) sets the value
.leftIcon(Icon) / .rightIcon(Icon) icons at the ends
.rightIcon(Icon, Action) clickable icon (e.g. clear)
.keyboard(Keyboard) keyboard type (see Masks and keyboards)
getText() / getTextWithoutMask() read value (with / without mask)
clearText() clear

Floating caption

Enable it in the style (floatingCaption) — the caption rises on focus/fill:

TextField email = new TextField().caption("E-mail");
email.<TextFieldStyle>getStyle().floatingCaption(true);

Password with show/hide button

TextField senha = new TextField().caption("Senha");
senha.<TextFieldStyle>getStyle()
   .mode(EditMode.PASSWORD)     // masks the characters
   .passwordToggle(true);       // adds the eye icon automatically

With passwordToggle(true), the icon on the right toggles the visibility on its own.

Action icon (e.g. clear)

TextField busca = new TextField().caption("Buscar")
   .rightIcon(new MaterialIcon(""), c -> busca.clearText());  // "close"

Best practices

  • Read the value with getText() at submit time; for raw digits use getTextWithoutMask().
  • Configure behavior (mode, maxLength, floatingCaption) via the typed getStyle() (campo.<TextFieldStyle>getStyle()...).
  • Limit the length with .maxLength(n) in the style when it makes sense.

Common mistakes

  • Looking for an onChange/onTextChanged — it does not exist; read getText() when you need to.
  • Confusing getText() (with mask) with getTextWithoutMask() (without) when saving to the database.
  • Setting mode/mask on the component — they are setters of the style.

See also

Was this page helpful?