Skip to content
CrossFly is 100% open sourceFollow us on GitHub
CrossFlyCrossFly
Get started
All examples
RecipesListsInput81 lines · 1 files

Choosing from a list

ListViewWindow does single and multi selection, with the title, the confirm and the tap-to-choose already wired.

What this shows

Picking one from a list, and picking several — without building either by hand.

This recipe exists because the reference app on this framework wrote its own single-choice sheet while ListViewWindow sat unused. Reading this one screen is cheaper than rediscovering it.

Run it on your machine

Every example is a plain Window, so the launcher only needs its name.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R06Select"
Editing it

Java is compiled ahead of time, so the browser cannot rebuild an edit on its own — the code you see here was turned into JavaScript by TeaVM before it was published. Copy the file into a project and it runs unchanged on all four platforms; the command above is the whole setup.

Running hereCrossFly 0.1.32
Starting the app…

Compiled to WebAssembly-era JavaScript by TeaVM — the same code, not a video of it. · All examples

What it uses

Framework types this example touches. The linked ones have a reference page.

ButtonComponentDebugLabelListViewWindowScreenStylesWidgets

The source

Complete and unabridged. This is the file, not an excerpt of it.

Raw
dev/cf/recipe/R06Select.java
package dev.cf.recipe;

import dev.cf.Tones;

import dev.crossfly.ui.*;
import dev.crossfly.ui.color.*;
import dev.crossfly.ui.graphics.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.style.*;

import static dev.crossfly.ui.Widgets.*;

/**
 * Picking one from a list, and picking several — without building either by hand.
 *
 * <p>This recipe exists because the reference app on this framework wrote its own single-choice
 * sheet while {@link ListViewWindow} sat unused. Reading this one screen is cheaper than
 * rediscovering it.</p>
 */
public class R06Select extends Recipe
{
   private static final String[] OPCOES = { "Beverages", "Food", "Cleaning", "Hygiene", "Bakery",
      "Frozen", "Pet" };

   private Label escolha;

   public String title()
   {
      return "Choosing from a list";
   }

   public String note()
   {
      return "ListViewWindow does single and multi selection, with the title, the confirm and the"
         + " tap-to-choose already wired.";
   }

   public void build(Component body) throws Exception
   {
      escolha = label("nothing chosen yet", 1f, false, Styles.captionColor, Position.START);
      escolha.testTag("recipe.choice");

      Button um = Tones.action("Pick one");
      um.testTag("recipe.pick.one");
      um.onAction(c -> abrir(ListViewWindow.ItemType.SINGLE));

      Button varios = new Button("Pick several");
      varios.testTag("recipe.pick.many");
      varios.onAction(c -> abrir(ListViewWindow.ItemType.MULTIPLE));

      body.add(LAY_AUTO, outgap(um, 0, 0, 0, .6f));
      body.add(LAY_AUTO, outgap(varios, 0, 0, 0, 1f));
      body.add(LAY_AUTO, escolha);
   }

   private void abrir(ListViewWindow.ItemType tipo)
   {
      try
      {
         final ListViewWindow janela = new ListViewWindow("Segment", OPCOES, tipo);
         // SINGLE closes on the tap; MULTIPLE waits for the confirm. Both arrive here.
         janela.onConfirm(c ->
         {
            int[] idx = janela.getSelectedIndices();
            StringBuilder sb = new StringBuilder();
            for (int i : idx)
               sb.append(sb.length() == 0 ? "" : ", ")
                  .append(OPCOES[i]);
            escolha.setText(sb.length() == 0 ? "nothing chosen" : sb.toString());
            escolha.setValidLayout(false);
            Screen.repaint();
         });
         janela.show();
      }
      catch (Exception e)
      {
         dev.crossfly.util.Debug.handle(e);
      }
   }
}
For an AI assistant

Machine-readable versions of this page, so a model can read the shelf instead of guessing at the API.

This example, as JSONThis example, as Markdown
PreviousListing: cards or tableNextPicking a date range