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

Reordering a list

The first row has no up arrow and the last has no down arrow — hidden, not removed, so the column does not jitter as rows move.

What this shows

A list whose order is the data: move rows, drop rows, read the order back.

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.R08Reorder"
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.

ComponentGridLayoutLabelReorderableListScreenStylesUtilsWidgets

The source

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

Raw
dev/cf/recipe/R08Reorder.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 java.util.*;

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

/**
 * A list whose order is the data: move rows, drop rows, read the order back.
 */
public class R08Reorder extends Recipe
{
   private ReorderableList lista;
   private Label ordem;

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

   public String note()
   {
      return "The first row has no up arrow and the last has no down arrow — hidden, not removed,"
         + " so the column does not jitter as rows move.";
   }

   public void build(Component body) throws Exception
   {
      ordem = label("", 1.1f, true, Tones.accent(), Position.START);
      ordem.testTag("recipe.order");

      lista = new ReorderableList()
         .items(new ArrayList<>(Arrays.asList("Name", "City", "Phone", "E-mail")))
         .renderer((i, key) ->
         {
            Component linha = new Component();
            linha.setLayout(new GridLayout(Position.START, Position.CENTER));
            linha.add(LAY_AUTO, LAY_AUTO, Tones.tile(key.substring(0, 1), Tones.cycle(i)),
               outgap(label(key, 1.2f, true, Styles.textColor, Position.START), .6f, 0, 0, 0));
            return linha;
         })
         // Fires AFTER the list is consistent, so it is safe to save straight from here.
         .onChange(this::mostrar);
      lista.testTag("recipe.list");

      body.add(LAY_AUTO, lista);
      body.add(LAY_AUTO, outgap(ordem, 0, 1f, 0, 0));
      mostrar();
   }

   private void mostrar()
   {
      ordem.setText(lista.items()
         .isEmpty() ? "(empty)" : dev.crossfly.util.Utils.join(" > ", lista.items()));
      ordem.setValidLayout(false);
      Screen.repaint();
   }
}
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
PreviousPicking a date rangeNextMaking a screen testable