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.

6. Handling input

What you’ll learn: reacting to touches, keys, and gestures, and reading text input. Prerequisites: Styling and themes.

Complete example (copy, paste, and run)

An add-task form: field + button with validation, reading the text on submit, and feedback with Hint. It adds the tasks to an output Label. Compiles as is.

package com.exemplo;

import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;

public class MainWindow extends Window
{
   private final TextField campo = new TextField().caption("Nova tarefa");
   private final Label saida = new Label("Nenhuma tarefa ainda");

   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      add(LAY_AUTO, campo);

      Button add = new Button("Adicionar");
      add.onAction(c -> {                                 // runs on the paint thread
         String texto = campo.getText().trim();
         if (texto.isEmpty()) { Hint.show("Digite algo", 2000); return; }   // validation (2 args)
         adicionarTarefa(texto);
         campo.clearText();
      });
      add(LAY_AUTO, add);

      add(LAY_AUTO, saida);
   }

   private void adicionarTarefa(String texto)
   {
      saida.setText("Adicionada: " + texto);
      saida.repaint();
   }
}

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

onAction vs onActionThread

botao.onAction(c -> salvar());          // runs on the paint thread (fast, touches the UI)
botao.onActionThread(c -> {             // runs on a background thread (heavy work)
   var dados = baixar();
   c.onUI(() -> mostrar(dados));         // back to the UI
});

Text input — read it on submit

There’s no per-key event: read getText() when you need it (e.g., in the button).

TextField campo = new TextField().caption("Nova tarefa");
Button add = new Button("Adicionar");
add.onAction(c -> {
   String texto = campo.getText().trim();
   if (texto.isEmpty()) { Hint.show("Digite algo", 2000); return; }   // validation
   adicionarTarefa(texto);
   campo.clearText();
});

Focus: override onFocus()/onBlur() in a subclass when you need to react to gaining/losing focus.

Touch gestures (overridable)

In a custom component, override the handlers for gestures:

@Override public void onLongTouch(float x, float y) throws Exception { abrirDetalhe(); }
// also: onTouchDown/onTouchUp/onPan

Exceptions in callbacks go to Debug.handle (they don’t bubble up to bring down the frame).

What you build in this step

The add-task form (field + button + validation) and the gesture to open the detail.

Recap

onAction (UI) / onActionThread (background); read text with getText() on submit; gestures via overridable handlers; errors go to Debug.handle.

Next steps

→ Managing state

See also

Was this page helpful?