Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Collections

A list, a table and a tree: three widgets over a model the application owns, that select by id, report what the user chose, and never edit the data.

The showcase's Collections screen: a virtualized list of ten thousand rows, a sortable table of the Company, a tree of the lands with cascading checkboxes, and a slotThe showcase's Collections screen: a virtualized list of ten thousand rows, a sortable table of the Company, a tree of the lands with cascading checkboxes, and a slot

The Collections screen of the showcase.

Each of the three takes the application’s own type and a few functions that describe it: how to identify an item, how to draw it, what to call it. The widget holds no copy of the data. Selection is a set of ids the application holds and hands back in, and every change arrives as the whole new set.

Important

A list, a table or a tree is built in Java. Markup can place one the model holds, with bind=, but cannot describe one: an item factory is code. The kdl samples below show the placing. The Java beside them is the widget.

list

A vertical list of rows, one per item, with a selection model and a keyboard.

A list of six stops from Hobbiton to Lothlórien, Rivendell highlightedA list of six stops from Hobbiton to Lothlórien, Rivendell highlighted

Six rows, one selected.

list bind="app.road" id="road" class="sidebar"
@Bind("app.road") private ListView<String> road =
        ListView.of(Leagues.ROAD)
                .virtualized()
                .selection(Selection.MULTIPLE)
                .selected(chosen, this::choose);

ListView.of(List<String>) is the short form: the string is the id, the label and the type-ahead text. The general constructor takes the application’s type and two functions:

new ListView<>(walkers, Walker::id, w -> new Text(w.name()))
        .text(Walker::name)
        .selected(picked, this::pick);

identity names an item, and that name is what selection carries. factory builds the row’s content, and any widget will do. text is optional and powers type-to-select.

Rows are virtualized by row height. virtualized() reads --gb-list-row-height from the cascade, so a compact density gets the right pitch. virtualized(double) names one. The list then builds only the rows a viewport can see, plus four either side, with a spacer above and below sized from the rest, so the showcase scrolls ten thousand rows. A list that is not virtualized builds every row.

The row the keyboard is on is built too, however far the list is scrolled from it, so the next arrow key carries on from where the focus was.

Rows that are not one height are virtualized by measuring them. virtualized(RowHeights.estimating(64)) builds the same window, measures each row as it is laid out, and counts every row it has not built at the estimate, so a timeline of five thousand messages costs a window per frame. Heights are remembered by each item’s identity, so rows prepended above keep theirs, and a change of width forgets them. When a row above the line being read turns out taller or shorter than it was counted, the enclosing scroll moves by the difference and the line stays where it was. A scroll with anchor="end" or preserve-on-prepend does that correcting itself. Home, End and type-to-select scroll the row they reach to the top. The rows carry the class measured and are as tall as their content, never shorter than --gb-list-row-height. Space rows with padding, because a row’s height is its border box and a margin or a gap between rows is not counted.

Warning

A virtualized list’s list-row height must be the pitch it was told. When a stylesheet makes them differ by more than half a pixel the list logs it once and the scroll range is wrong.

Attributes

AttributeTypeDefaultWhat it does
bindpathnoneThe ListView a model holds. Nothing bound draws nothing.
id, classstringLaid over the widget’s own id and classes.

Nothing else is read, and children are ignored.

Java

MethodWhat it does
ListView.of(List<String>)A list of strings, each its own id and label.
new ListView<>(items, identity, factory)The general form.
selection(Selection)NONE, SINGLE or MULTIPLE. Single is the default.
selected(Set<String>, Consumer<Set<String>>)The current selection and where to report the next.
selected(String, Consumer<String>)The same for a single selection.
text(Function<T, String>)What type-to-select matches.
virtualized(), virtualized(double)Build only the visible window.
virtualized(RowHeights)Build only the visible window of rows that vary in height, measured as they are built.
sections(Function<T, String>, Function<String, Widget>)Headed runs of items, with the top one pinned.
itemMenu(Function<T, String>)A context-menu name per item.

In MULTIPLE, a plain press replaces the selection, Ctrl toggles the row, and Shift selects from the last plain press through to the row. What is reported is the whole set, in the order it was chosen.

Styling

The CSS type is list. Each row is a list-row, and the virtual spacers are list-spacer. A row matches :hover and :focus-visible, and carries the class selected rather than a pseudo-class. The row height is --gb-list-row-height, 32px, 26px at compact density.

Keyboard

KeyDoes
Up, Downmove focus between rows, without selecting, and scroll the row into view. They wrap at the ends
Enterselects the focused row. Ctrl+Enter toggles, Shift+Enter ranges
Home, Endthe first and last item, scrolled into view
letterstype-to-select, when text is set. One second between letters, repeat a letter to cycle

A right-click selects the row it is over before the context menu opens, unless it is already selected.

table

A list with columns: one row per item, one cell per column, and a header that sorts by asking.

A table with Name, Realm and Leagues columns and four rows of the CompanyA table with Name, Realm and Leagues columns and four rows of the Company

Three columns, the last a fixed width.

table bind="app.company" id="company"
new Table<>(sorted, Walker::id, List.of(
        Column.<Walker>of("name", "Name", Walker::name).sortable(true).resizable(true),
        Column.<Walker>of("realm", "Realm", Walker::realm).sortable(true).weight(2),
        Column.<Walker>of("leagues", "Leagues", w -> String.valueOf(w.leagues()))
                .sortable(true)
                .fixed(96)
))
    .sorted(sort, this::sortBy)
    .selection(Selection.MULTIPLE)
    .selected(picked, this::pick)
    .id("company");

A Column has a key, a header, a cell function and a width. Column.of draws text, Column.widget draws any widget. weight(double) shares the remaining width, fixed(double) takes pixels.

Sorting is reported, not performed. A press on a sortable header calls onSort with the Sort the table would like next: the column, ascending, or the same column flipped. The application sorts its items and rebuilds, and the caret appears on the column sort names.

private void sortBy(Sort next) {
    setState(() -> sort = next);   // and sorted() orders the items by it
}

A resizable column has a grip after its header. Dragging it asks onResize for a new width, and the application decides what the column becomes. The header stays at the top while the rows scroll.

Attributes

AttributeTypeDefaultWhat it does
bindpathnoneThe Table a model holds. Nothing bound draws nothing.
id, classstringLaid over the widget’s own.

A column child in markup is the layout widget, not a column definition, and is discarded.

Java

MethodWhat it does
new Table<>(items, identity, columns)The table.
sorted(Sort, Consumer<Sort>)The current sort and where to report the next. null is model order.
resized(BiConsumer<String, Double>)Where a grip reports a column key and a width.
selection, selectedAs list.
virtualized(double)Build only the visible rows, at this pitch.

Sort.by(column) is ascending. Sort.next(current, column) is what the header asks for.

Styling

The CSS type is table. The header is a table-head of table-header cells, each holding a table-label and, on the sorted column, a sort-caret; a table-rule runs under it, and a table-grip sits after a resizable header. Rows are the list’s list-row, each a table-cells of table-cell. A header carries the classes sortable, sorted and ascending or descending, and matches :hover and :focus-visible. The header height is --gb-table-header-height, 36px, 30px at compact density.

Keyboard

Rows take what list takes: Up, Down, Enter, Home, End. A sortable header is a Tab stop, and Enter or Space on it asks for the next sort. A table has no type-to-select.

tree

A list that remembers what is open: nodes with children, a chevron on those that have or may have some, and the list’s selection models.

A tree with two closed branches, Eriador and Erebor, each with a checkboxA tree with two closed branches, Eriador and Erebor, each with a checkbox

Two roots with cascading checkboxes.

tree bind="app.lands" id="lands"
List<TreeNode> lands = List.of(
        TreeNode.of("eriador", "Eriador",
                TreeNode.leaf("shire", "The Shire"),
                TreeNode.leaf("bree", "Bree")),
        TreeNode.lazy("erebor", "Erebor",
                () -> List.of(TreeNode.leaf("dale", "Dale")))
);

new Tree(lands, selected, this::select)
        .checkable(Checkable.CASCADE)
        .checked(checked, this::check)
        .id("lands");

The model is a TreeNode. It has an id, a label and either a list of children or a supplier of them. TreeNode.leaf has none, TreeNode.of has them up front, and TreeNode.lazy fetches them the first time the node opens. The tree keeps which ids are open and what a supplier returned, so a rebuild with the same ids keeps the same shape.

Checking and selecting are two different things. Selection is the list’s, a set of ids reported whole. checkable(Checkable) adds a checkbox per node: LEAF on leaves only, ANY on every node, CASCADE where a parent’s box shows its children’s state and checking it checks them all. The checked set is reported through checked(Set, Consumer) and never touches selection.

Attributes

AttributeTypeDefaultWhat it does
bindpathnoneThe Tree a model holds. Nothing bound draws nothing.
id, classstringLaid over the widget’s own.

There is no checkable= or node child in markup. Those are the model’s.

Java

MethodWhat it does
new Tree(roots, selected, onSelect)Single selection of leaves only.
anyNode(true)Lets a parent be selected too.
selection(Selection), selected(Set), onSelect(Consumer)The list’s models.
checkable(Checkable)NONE, LEAF, ANY or CASCADE.
checked(Set<String>, Consumer<Set<String>>)The checked ids and where to report the next set.

A tree is not virtualized: every visible row is built.

Styling

The CSS type is tree. Each row is a tree-row holding a tree-indent, a tree-chevron, a tree-check when the tree is checkable, and a tree-label. A row carries the classes expanded, selected and group, the last on a row that cannot be selected, and matches :hover and :focus-visible. The indent is 20px per level and the row height is --gb-list-row-height.

Keyboard

KeyDoes
Up, Downmove focus between visible rows
Rightopens a closed node, or moves to the first child of an open one
Leftcloses an open node, or moves to the parent
Enterselects, with Ctrl and Shift as the list has them
Spacetoggles the row’s checkbox
Home, Endthe first and last visible row
*opens every sibling of the focused row
letterstype-to-select over the visible rows

A click on the chevron opens or closes without selecting. A click on a parent that cannot be selected opens it.

slot

A region of a document whose content the model decides: whatever widget a bind= value holds.

A card reading Samwise Gamgee, with The Shire, 1795 leagues under itA card reading Samwise Gamgee, with The Shire, 1795 leagues under it

The widget the model holds, drawn where the document put it.

slot bind="app.detail" id="detail" class="pane"
@Bind("app.detail") private Widget detail = new Text("Nothing selected");

void select(Person person) {
    detail = new PersonForm(person);   // the region redraws as the form
}

list, table and tree place a widget of their own kind. slot places any widget: a detail pane that is a form for one selection and a message for none, a toolbar that changes with the mode. The model holds the widget it builds and replaces it when the region should change. The element subscribes to the binding, so the region rebuilds and whatever is under it reconciles by key.

Attributes

AttributeTypeDefaultWhat it does
bindpathnoneThe widget a model holds. Nothing bound, or a value that is not a widget, draws nothing.
id, classstringLaid over the widget’s own id and classes, when it has any.

Children are ignored. A slot has no CSS type of its own: what a stylesheet sees is the widget it draws.