This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.
How to show a modal selection popup
Purpose. Open a selection list as a floating window (not embedded in the layout).
ListViewWindow wraps a ListView inside a Window, centers it on screen, and closes
itself when the user makes a selection. It works both for simple selection and for rows with
a custom appearance.
APIs used
new ListViewWindow(String[] items, ListViewWindow.ItemType type)— creates the popup; there is also the overload withString title.ListViewWindow.ItemType.SINGLE/MULTIPLE— own enum ofListViewWindow(alias ofListView.ItemType, converted internally bytoPanel()).ListViewWindow.size(float widthRatio, float heightRatio)— size as a fraction of the screen (defaults0.5f,0.5f); the popup is always centered.ListViewWindow.onConfirm(Component.Action)— delegates to the internalListView; on confirm in SINGLE, the window closes itself viacloseAction(this::hide).ListViewWindow.ItemFactory(ItemFactory factory)— note: method with an uppercase initial. The interface isItemFactory { Component create(String text, int index) throws Exception; }.ListViewWindow.show()/hide()— inherited fromWindow(pushes/pops on theEventsProcessor).ListViewWindow.getSelectedText()/getSelectedIndex()/getSelectedTexts()— delegated to the internal panel.ListViewWindow.getPanel()— access to the underlyingListView, if you need advanced config.
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());
final String[] paises = {"Brasil", "Portugal", "Angola", "Moçambique"};
// ListViewWindow is a popup: does NOT go into the layout — opens with show() fired by a Button
Button abrir = new Button("Escolher país");
abrir.onClick(e ->
{
ListViewWindow popup = new ListViewWindow("País", paises, ListViewWindow.ItemType.SINGLE);
popup.size(.6f, .5f); // 60% of the screen width, 50% of the height
popup.onConfirm(ev ->
{
String pais = popup.getSelectedText(); // the window already closes itself on SINGLE
Debug.log("País: " + pais);
return null;
});
popup.show(); // opens a centered modal
return null;
});
add(LAY_AUTO, abrir);
}
}
The snippet below shows only the core. To run it, place it inside the
MainWindowskeleton above.
Minimal snippet
// verified in source: ListViewWindow.java
String[] paises = {"Brasil", "Portugal", "Angola", "Moçambique"};
ListViewWindow popup = new ListViewWindow("País", paises, ListViewWindow.ItemType.SINGLE);
popup.size(.6f, .5f); // 60% of the screen width, 50% of the height
popup.onConfirm(e ->
{
String pais = popup.getSelectedText(); // the window already closes itself on SINGLE
return null;
});
popup.show(); // opens a centered modal
Custom rows with ItemFactory (note the uppercase I in the method name):
// verified in source: ListViewWindow.ItemFactory (interface) + ItemFactory(...) method
ListViewWindow popup = new ListViewWindow("Contato", nomes, ListViewWindow.ItemType.SINGLE);
popup.ItemFactory((text, index) ->
{
Component linha = new Component();
linha.setLayout(new GridLayout());
linha.add(new Label(text)); // build the row however you like
return linha;
});
popup.onConfirm(e -> { /* ... */ return null; });
popup.show();
Javadoc × code discrepancy: the class Javadoc shows
sel.show()on aListView, butshow()/hide()exist onWindow— therefore onListViewWindow, not onListView. For the popup mode always useListViewWindow.
Go deeper
- Embedded (windowless) version of the same list: lista-simples.md.
- Widget guide: ListViewWindow · Canonical source: Chapter 15