ChoiceBox
The ChoiceBox
component is a user interface element designed to present users with a list of options or choices. Users can select a single option from this list, typically by clicking the ChoiceBox
, which triggers the display of a dropdown list containing available choices. Users can also interact with the ChoiceBox
with the arrow keys. When a user makes a selection, the chosen option is then displayed in the ChoiceBox
button.
Usages
ChoiceBox
components are used for various purposes, such as selecting items from a menu, choosing from a list of categories, or picking options from predefined sets. They provide an organized and visually pleasing way for users to make selections, particularly when there are multiple options available. Common usages include:
- User Selection of Options: The primary purpose of a
ChoiceBox
is to allow users to select a single option from a list. This is valuable in applications that require users to make choices, such as:
- Selecting items from a list
- Choosing from a list of categories
- Picking options from predefined sets
-
Form Inputs: When designing forms that require users to input specific options, the
ChoiceBox
simplifies the selection process. Whether it's selecting a country, state, or any other option from a predefined list, theChoiceBox
streamlines the input process. -
Filtering and Sorting:
ChoiceBox
can be employed for filtering and sorting tasks in applications. Users can choose filter criteria or sorting preferences from a list, facilitating the organization and navigation of data. -
Configuration and Settings: When your application includes settings or configuration options, the
ChoiceBox
provides an intuitive way for users to adjust preferences. Users can pick settings from a list, making it easy to tailor the application to their needs.
The ChoiceBox
is intended for use when a preset number of options are available, and custom options should not be allowed or included. If allowing users to enter custom values is desired, use a ComboBox
instead
Constructors
- : Constructs a new
ChoiceBox
without a label. - : Constructs a new
ChoiceBox
with the specified label. - : Constructs a new
ChoiceBox
with the given label and a listener to handle item selection events.
Dropdown Type
Using the
method will assign a value to thetype
attribute of a ChoiceBox
, and a corresponding value for the data-dropdown-for
attribute in the dropdown of the ChoiceBox
. This is helpful for styling, as the dropdown is taken out of its current position in the DOM and relocated to the end of the page body when opened.
This detachment creates a situation where directly targeting the dropdown using CSS or shadow part selectors from the parent component becomes challenging, unless you make use of the dropdown type attribute.
In the demo below, the Dropdown type is set and used in the CSS file to select the dropdown and change the background color.
Show Code
- Java
- CSS
Max Row Count
By default, the number of rows displayed in the dropdown of a ChoiceBox
will be increased to fit the content. However, using the
Using a number that is less than or equal to 0 will result in unsetting this property.
Show Code
- Java
Opening Dimensions
The ChoiceBox
component has methods that allow manipulation of the dropdown dimensions. The maximum height and minimum width of the dropdown can be set using the
Passing a String
value to either of these methods will allow for any valid CSS unit to be applied, such as pixels, viewport dimensions, or other valid rules. Passing an int
will set the value passed in pixels.
Styling
Shadow Parts
These are the various parts of the shadow DOM for the ChoiceBox
component, which will be required when styling via CSS is desired.
CSS Properties
These are the various CSS properties that are used in the component, with a short description of their use.
Reflected Attributes
The reflected attributes of a component will be shown as attributes in the rendered HTML element for the component in the DOM. This means that styling can be applied using these attributes.
Dependencies
This component relies on the following components - see the related article for more detailed styling information:
Best Practices
To ensure an optimal user experience when using the ChoiceBox
component, consider the following best practices:
-
Clear and Limited Options: Keep the list of choices concise where possible, and relevant to the user's task. A
ChoiceBox
is ideal for presenting a clear list of options. -
User-Friendly Labels: Ensure that the displayed labels for each option are user-friendly and self-explanatory. Make sure users can easily understand the purpose of each choice.
-
Default Selection: Set a default selection when the ChoiceBox is initially displayed. This ensures a pre-selected option, reducing the number of interactions required to make a choice.
-
ChoiceBox vs. Other List Components: A
ChoiceBox
should be used over other List components in the following situations:
- Multiple Selection is not needed or required, and all items do not need to be displayed at once.
- A selection of predefined choices is desired, preventing users from entering custom or unexpected data.