Docs

Documentation versions (currently viewingVaadin 24)

List Box

List Box allows the user to select one or more values from a scrollable list of items.

List Box allows the user to select one or more values from a scrollable list of items.

Open in a
new tab
MultiSelectListBox<String> listBox = new MultiSelectListBox<>();
listBox.setItems(SHOW_ASSIGNEE, SHOW_DUE_DATE, SHOW_STATUS);
listBox.select(SHOW_ASSIGNEE, SHOW_STATUS);

Although functionally similar to Checkbox Group and Radio Button Group, List Box is designed to be used as a lightweight scrollable selection list, rather than as a form input field.

Dividers

You can use dividers to group related items. Use them sparingly to avoid creating unnecessary visual clutter.

Open in a
new tab
listBox.setItems(SHOW_ASSIGNEE, SHOW_DUE_DATE, SHOW_STATUS,
        SHOW_THUMBNAIL, SHOW_PREVIEW);
listBox.addComponents(SHOW_STATUS, new Hr());

Disabled Items

Disable items to show that they are currently unavailable for selection.

Open in a
new tab
ListBox<Status> listBox = new ListBox<>();
listBox.setItems(inProgress, done, cancelled);
listBox.setItemEnabledProvider(status -> status.getCount() > 0);
Note
Accessibility

Some assistive technologies don’t announce disabled items.

Selection

List Box supports both single and multiple selection. Single selection allows the user to select only one item, whereas multiple selection enables more than one item to be selected.

Single

Open in a
new tab
ListBox<String> listBox = new ListBox<>();
listBox.setItems(IN_PROGRESS, DONE, CANCELLED);
listBox.setValue(IN_PROGRESS);

Multi

Open in a
new tab
MultiSelectListBox<Person> listBox = new MultiSelectListBox<>();
listBox.setItems(items);
listBox.select(items.get(0), items.get(3));

Custom Item Presentation

Items can be rendered with rich content instead of plain text. This can be useful to provide additional information in a more legible fashion than appending it to the item text.

Open in a
new tab
listBox.setRenderer(new ComponentRenderer<>(person -> {
    HorizontalLayout row = new HorizontalLayout();
    row.setAlignItems(FlexComponent.Alignment.CENTER);

    Avatar avatar = new Avatar();
    avatar.setName(person.getFullName());
    avatar.setImage(person.getPictureUrl());

    Span name = new Span(person.getFullName());
    Span profession = new Span(person.getProfession());
    profession.getStyle()
            .set("color", "var(--lumo-secondary-text-color)")
            .set("font-size", "var(--lumo-font-size-s)");

    VerticalLayout column = new VerticalLayout(name, profession);
    column.setPadding(false);
    column.setSpacing(false);

    row.add(avatar, column);
    row.getStyle().set("line-height", "var(--lumo-line-height-m)");
    return row;
}));

Best Practices

List Box isn’t designed to be used as an input field in forms, and lacks features such as label, helper, and validation errors. See related components later for better options for use in forms. List Box is best suited for use as a lightweight, scrollable, single-column list for single or multi-selection of items.

Component Usage recommendations

Checkbox Group

Input field for selecting multiple options from a list.

Combo Box

Select a value from a filterable overlay. Appropriate for large sets of options. Supports lazy loading and entry of custom values.

Radio Button Group

Select a single option from a list. Optimal accessibility, as all options are visible without any user action.

Select

Input field for selecting a value from a overlay. More compact than a Radio Button Group.

Grid

A more advanced list component for cases where multiple columns, filtering or lazy loading is required.

30C8BEB8-5F4C-4CF7-B292-AE67C4151CBC