Combo Box
Combo Box allows the user to choose a value from a filterable list of options presented in an overlay. It supports lazy loading and can be configured to accept custom typed values.
new tab
ComboBox<Country> comboBox = new ComboBox<>("Country");
comboBox.setItems(DataService.getCountries());
comboBox.setItemLabelGenerator(Country::getName);
add(comboBox);
The overlay opens when the user clicks the field using a pointing device. Using the Up/Down arrow keys or typing a character (found in at least one of the options) when the field is focused also opens the popup.
Custom Value Entry
Combo Box can be configured to allow entering custom values that are not included in the list of options.
new tab
ComboBox<String> comboBox = new ComboBox<>("Browser");
comboBox.setAllowCustomValue(true);
add(comboBox);
Allowing custom entry is useful when you need to present the most common choices but still give users the freedom to enter their own options.
Custom values can also be stored and added to the list of options:
new tab
ComboBox<String> comboBox = new ComboBox<>("Browser");
comboBox.setAllowCustomValue(true);
comboBox.addCustomValueSetListener(e -> {
String customValue = e.getDetail();
if (items.contains(customValue)) return;
items.add(customValue);
comboBox.setItems(items);
comboBox.setValue(customValue);
});
add(comboBox);
Custom Item Presentation
Items can be customised to display more information than a single line of text.
new tab
ComboBox<Person> comboBox = new ComboBox<>("Choose doctor");
comboBox.setItems(filter, DataService.getPeople());
comboBox.setItemLabelGenerator(person -> person.getFirstName() + " " + person.getLastName());
comboBox.setRenderer(getRenderer());
comboBox.getStyle().set("--vaadin-combo-box-overlay-width", "16em");
add(comboBox);
...
// NOTE
// We are using inline styles here to keep the example simple.
// We recommend placing CSS in a separate style sheet and to
// encapsulating the styling in a new component.
private TemplateRenderer<Person> getRenderer() {
StringBuilder tpl = new StringBuilder();
tpl.append("<div style=\"display: flex;\">");
tpl.append(" <img style=\"height: var(--lumo-size-m); margin-right: var(--lumo-space-s);\" src=\"[[item.pictureUrl]]\" alt=\"Portrait of [[item.firstName]] [[item.lastName]]\" />");
tpl.append(" <div>");
tpl.append(" [[item.firstName]] [[item.lastName]]");
tpl.append(" <div style=\"font-size: var(--lumo-font-size-s); color: var(--lumo-secondary-text-color);\">[[item.profession]]</div>");
tpl.append(" </div>");
tpl.append("</div>");
return TemplateRenderer.<Person>of(tpl.toString()).withProperty("pictureUrl", Person::getPictureUrl)
.withProperty("firstName", Person::getFirstName).withProperty("lastName", Person::getLastName)
.withProperty("profession", Person::getProfession);
}
Use a custom filter to allow the user to search by the rendered properties. It is recommended to make filtering case insensitive.
Auto Open
The overlay opens automatically when the field is focused using a pointer (mouse or touch), or when the user types in the field. You can disable that to only open the overlay when the toggle button or Up/Down arrow keys are pressed.
new tab
ComboBox<Country> comboBox = new ComboBox<>("Country");
comboBox.setAutoOpen(false);
add(comboBox);
Popup Width
The width of the popup is, by default, the same width as the input field. The popup width can be overridden to any fixed width in cases where the default width is too narrow.
new tab
ComboBox<Person> comboBox = new ComboBox<>("Employee");
comboBox.getStyle().set("--vaadin-combo-box-overlay-width", "350px");
add(comboBox);
Placeholder
Use the placeholder feature to provide an inline text prompt for the field. Do not create, or use, a separate item for this purpose.
new tab
ComboBox<Person> comboBox = new ComboBox<>("Employee");
comboBox.setPlaceholder("Select employee");
add(comboBox);
Custom Filtering
Combo Box’s filtering, by default, is configured to only show items that contain the entered value:
new tab
import { html, LitElement, customElement, state } from 'lit-element';
import '@vaadin/vaadin-combo-box/vaadin-combo-box';
import { applyTheme } from 'Frontend/generated/theme';
import countries from '../../../../src/main/resources/data/countries.json';
interface Country {
readonly name: string;
readonly abbreviation: string;
readonly id: string;
}
@customElement('combo-box-filtering-1')
export class Example extends LitElement {
constructor() {
super();
// Apply custom theme (only supported if your app uses one)
applyTheme(this.shadowRoot);
}
@state()
private items: Country[] = countries;
render() {
return html`
<vaadin-combo-box
label="Country"
item-label-path="name"
item-value-path="id"
.items="${this.items}"
></vaadin-combo-box>
`;
}
}
Custom filtering is also possible. For example, if we only want to show items that start with the user’s input:
new tab
ComboBox<Country> comboBox = new ComboBox<>("Country");
ItemFilter<Country> filter = (country, filterString) -> country.getName().toLowerCase().startsWith(filterString.toLowerCase());
comboBox.setItems(filter, DataService.getCountries());
add(comboBox);
Usage as Autocomplete Field
As the user is typing, the Combo Box will filter out the options that don’t match. Once the correct value has been found, the user can use the Up/Down arrow keys to navigate the list and the Enter key to set the value, essentially using the Combo Box as an autocomplete field.
Best Practices
Combo Box supports lazy loading for large datasets. It reduces the initial load time, consumes less bandwidth and resources.
Note
|
Do not use as a menu
Combo Box is an input field component, not a generic menu component.
Use the Menu Bar component to create overlays for actions.
|
Related Components
Component | Usage recommendations |
---|---|
Simpler overlay selection field without filtering, lazy loading or custom value entry. | |
Better accessibility than Combo Box, as all options are visible without user interaction. | |
Scrollable inline list of options. Supports single and multi-select. | |
Overlay menus for items that trigger actions. |
475FE91A-59C1-46FC-908E-388948F6E114