This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.
How to make a simple selectable list
Purpose. Show a scrollable list of text items and react when the user
selects one (SINGLE) or several (MULTIPLE). The ListView is an embeddable Component: you
add it to any layout, with no window needed.
APIs used
new ListView(String[] items, ListView.ItemType type)— creates the list from an array of texts.new ListView(String title, String[] items, ListView.ItemType type)— same, with a title header.ListView.ItemType.SINGLE/ListView.ItemType.MULTIPLE— the only two enum values.SINGLE= tap selects and firesonConfirm;MULTIPLE= checkboxes + “Confirm” button.ListView.onConfirm(Component.Action action)— callback fired on confirm. Receives theListViewitself as the event source.ListView.getSelectedText()/getSelectedIndex()— first selected item (text/index), ornull/-1.ListView.getSelectedTexts()/getSelectedIndices()— all selected items (useful in MULTIPLE).ListView.confirmText(String)— text of the confirmation button (default"Confirmar", MULTIPLE only).
Complete example (copy, paste and run)
package com.exemplo;
import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
/** 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());
String[] frutas = {"Maçã", "Banana", "Laranja", "Uva"};
// SINGLE: the tap already selects and fires onConfirm
ListView lista = new ListView("Escolha uma fruta", frutas, ListView.ItemType.SINGLE);
lista.onConfirm(e ->
{
String texto = lista.getSelectedText(); // e.g.: "Banana"
int idx = lista.getSelectedIndex(); // e.g.: 1
Debug.log("Selecionado: " + texto + " (#" + idx + ")");
return null;
});
add(LAY_FILL, lista); // ListView is a Component: goes straight into the layout
}
}
The snippet below shows only the core. To run it, place it inside the
MainWindowskeleton above.
Minimal snippet
// verified in source: ListView.java
String[] frutas = {"Maçã", "Banana", "Laranja", "Uva"};
// SINGLE: the tap already selects and fires onConfirm
ListView lista = new ListView("Escolha uma fruta", frutas, ListView.ItemType.SINGLE);
lista.onConfirm(e ->
{
String texto = lista.getSelectedText(); // e.g.: "Banana"
int idx = lista.getSelectedIndex(); // e.g.: 1
Debug.log("Selecionado: " + texto + " (#" + idx + ")");
return null;
});
layout.add(.9f, lista); // ListView is a Component: goes straight into the layout
// MULTIPLE: checkboxes; onConfirm fires only when tapping the "Confirm" button
ListView multi = new ListView("Selecione ingredientes", frutas, ListView.ItemType.MULTIPLE);
multi.confirmText("Adicionar");
multi.onConfirm(e ->
{
String[] escolhidas = multi.getSelectedTexts(); // all the checked ones
return null;
});
layout.add(.9f, multi);
Go deeper
- Need a modal popup instead of embedded? See popup-de-selecao.md.
- Thousands of items? Don’t use
ListView(it materializes them all); see lista-virtual-grande.md. - Widget guide: ListView · Canonical source: Chapter 15
Was this page helpful?Thanks for the feedback!