Saltar al contenido
CrossFly es 100% open sourceSíguenos en GitHub
CrossFlyCrossFly
Empezar

Esta página fue traducida automáticamente del portugués y puede contener errores. La versión en portugués es la canónica.

Cómo hacer una lista simple seleccionable

Propósito. Mostrar una lista desplazable de elementos de texto y reaccionar cuando el usuario selecciona uno (SINGLE) o varios (MULTIPLE). El ListView es un Component incrustable: lo añades a cualquier layout, sin necesidad de ventana.

APIs usadas

  • new ListView(String[] items, ListView.ItemType type) — crea la lista a partir de un array de textos.
  • new ListView(String title, String[] items, ListView.ItemType type) — igual, con encabezado de título.
  • ListView.ItemType.SINGLE / ListView.ItemType.MULTIPLE — los únicos dos valores del enum. SINGLE = tap selecciona y dispara onConfirm; MULTIPLE = checkboxes + botón “Confirmar”.
  • ListView.onConfirm(Component.Action action) — callback disparado al confirmar. Recibe el propio ListView como fuente del evento.
  • ListView.getSelectedText() / getSelectedIndex() — primer elemento seleccionado (texto/índice), o null/-1.
  • ListView.getSelectedTexts() / getSelectedIndices() — todos los seleccionados (útil en MULTIPLE).
  • ListView.confirmText(String) — texto del botón de confirmación (default "Confirmar", solo en MULTIPLE).

Ejemplo completo (copiar, pegar y ejecutar)

package com.exemplo;

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

/** Pantalla principal ejecutable. Apunta el launcher (classname) y corre: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      String[] frutas = {"Maçã", "Banana", "Laranja", "Uva"};

      // SINGLE: el tap ya selecciona y dispara onConfirm
      ListView lista = new ListView("Escolha uma fruta", frutas, ListView.ItemType.SINGLE);
      lista.onConfirm(e ->
      {
         String texto = lista.getSelectedText();   // ej.: "Banana"
         int idx = lista.getSelectedIndex();       // ej.: 1
         Debug.log("Selecionado: " + texto + " (#" + idx + ")");
         return null;
      });
      add(LAY_FILL, lista);   // ListView es un Component: entra directo en el layout
   }
}

El fragmento de abajo muestra solo el núcleo. Para ejecutarlo, colócalo en el esqueleto MainWindow de arriba.

Fragmento mínimo

// verificado en la fuente: ListView.java
String[] frutas = {"Maçã", "Banana", "Laranja", "Uva"};

// SINGLE: el tap ya selecciona y dispara onConfirm
ListView lista = new ListView("Escolha uma fruta", frutas, ListView.ItemType.SINGLE);
lista.onConfirm(e ->
{
   String texto = lista.getSelectedText();   // ej.: "Banana"
   int idx = lista.getSelectedIndex();       // ej.: 1
   Debug.log("Selecionado: " + texto + " (#" + idx + ")");
   return null;
});
layout.add(.9f, lista);   // ListView es un Component: entra directo en el layout
// MULTIPLE: checkboxes; onConfirm dispara solo al tocar el botón "Confirmar"
ListView multi = new ListView("Selecione ingredientes", frutas, ListView.ItemType.MULTIPLE);
multi.confirmText("Adicionar");
multi.onConfirm(e ->
{
   String[] escolhidas = multi.getSelectedTexts();   // todos los marcados
   return null;
});
layout.add(.9f, multi);

Profundiza

¿Te resultó útil esta página?