Skip to main content

Lists

info

This section describes common features of all list components, and isn't a class that can be instantiated or used directly.

There are three types of lists available for use within your applications: ListBox, ChoiceBox, and ComboBox. These components all display a list of key-value items, and provide methods to add, remove, select, and manage the items within the list.

This page outlines the shared features and behavior of all list components, while specific details for each are covered in their respective pages.

Using ListItem

List components are composed of ListItem objects, which represent individual items within a list. Each ListItem is associated with a unique key and display text. Important features of the ListItem class include:

  • A ListItem encapsulates a unique key Object and a text String to display within the list component.
  • You can construct a ListItem by providing a key and text, or by specifying only the text so that a random key is generated.

Managing ListItem objects with the API

The various List components offer several methods for managing the list of items and maintaining a consistent state between the list and the client. By using these methods, you can effectively manage the items within the list. The API allows you to interact with and manipulate the list to meet your app's requirements.

Adding items

tip

To optimize performance, instead of triggering a server-to-client message each time you use the add() method, it's more efficient to create a List of ListItem objects first. Once you have this list, you can add them all at once using the insert(int index, List<ListItem> items) method. This approach reduces server-client communication, enhancing overall efficiency. For detailed guidelines on this and other best practices in webforJ architecture, refer to Client/Server Interaction.

Removing items

Other list operations

Iterating over lists

All List components implement the Java Iteratable interface, providing an efficient and intuitive way to iterate through a list's contents. With this interface, you can easily loop through every ListItem, making it simple to access, modify, or perform actions on each item with minimal effort. The Iterable interface is a standard pattern of the Java language, ensuring your code is familiar and maintainable for any Java developer.

The code snippet below demonstrates two easy ways to iterate through a list:

list.forEach(item -> {
item.setText("Modified: " + item.getText());
});

for (ListItem item : list) {
item.setText("Modified2: " + item.getText());
}

Shared list properties

Label

All List components can be assigned a label, which is a descriptive text or title associated with the component. Labels provide a brief explanation or prompt to help users understand the purpose or expected selection for that particular list. In addition to their importance for usability, list labels also play a crucial role in accessibility, enabling screen readers and assistive technologies to provide accurate information and facilitate keyboard navigation.

Helper text

Each List component can display helper text beneath the list using the setHelperText() method. This helper text offers additional context or explanations about the available options, ensuring users have the necessary information to make informed selections.

Topics

📄️ ComboBox

The ComboBox component is a user interface element designed to present users with a list of options or choices, as well as a field for entering their own custom values. Users can select a single option from this list, typically by clicking the ComboBox, which triggers the display of a dropdown list containing available choices, or type in a custom value. Users can also interact with the ComboBox with the arrow keys. When a user makes a selection, the chosen option is then displayed in the ComboBox.