Class ComboBox<T>

    • Constructor Detail

      • ComboBox

        public ComboBox​(int pageSize)
        Creates an empty combo box with the defined page size for lazy loading.

        The default page size is 50.

        The page size is also the largest number of items that can support client-side filtering. If you provide more items than the page size, the component has to fall back to server-side filtering.

        Parameters:
        pageSize - the amount of items to request at a time for lazy loading
      • ComboBox

        public ComboBox()
        Default constructor. Creates an empty combo box.
      • ComboBox

        public ComboBox​(String label)
        Creates an empty combo box with the defined label.
        Parameters:
        label - the label describing the combo box
      • ComboBox

        public ComboBox​(String label,
                        Collection<T> items)
        Creates a combo box with the defined label and populated with the items in the collection.
        Parameters:
        label - the label describing the combo box
        items - the items to be shown in the list of the combo box
        See Also:
        setItems(Collection)
      • ComboBox

        @SafeVarargs
        public ComboBox​(String label,
                        T... items)
        Creates a combo box with the defined label and populated with the items in the array.
        Parameters:
        label - the label describing the combo box
        items - the items to be shown in the list of the combo box
        See Also:
        HasListDataView.setItems(Object...)
    • Method Detail

      • setRenderer

        public void setRenderer​(Renderer<T> renderer)
        Sets the Renderer responsible to render the individual items in the list of possible choices of the ComboBox. It doesn't affect how the selected item is rendered - that can be configured by using setItemLabelGenerator(ItemLabelGenerator).
        Parameters:
        renderer - a renderer for the items in the selection list of the ComboBox, not null Note that filtering of the ComboBox is not affected by the renderer that is set here. Filtering is done on the original values and can be affected by setItemLabelGenerator(ItemLabelGenerator).
      • setItems

        public ComboBoxListDataView<T> setItems​(Collection<T> items)
        Sets the items from the given Collection and returns a ListDataView that provides information and allows operations on the items.

        Filtering will use a case insensitive match to show all items where the filter text is a substring of the label displayed for that item, which you can configure with setItemLabelGenerator(ItemLabelGenerator).

        Filtering will be handled in the client-side if the size of the data set is less than the page size. To force client-side filtering with a larger data set (at the cost of increased network traffic), you can increase the page size with setPageSize(int).

        Setting the items resets the combo box's value to null.

        Specified by:
        setItems in interface HasListDataView<T,​ComboBoxListDataView<T>>
        Parameters:
        items - the items to display, not null
        Returns:
        ListDataView providing access to the items
      • setItems

        public ComboBoxListDataView<T> setItems​(ComboBox.ItemFilter<T> itemFilter,
                                                Collection<T> items)
        Sets the data items of this combo box and a filtering function for defining which items are displayed when user types into the combo box.

        Note that defining a custom filter will force the component to make server roundtrips to handle the filtering. Otherwise it can handle filtering in the client-side, if the size of the data set is less than the pageSize.

        Setting the items resets the combo box's value to null.

        The returned data view object can be used for further access to combo box items, or later on fetched with getListDataView(). For using lazy data loading, use one of the setItems methods which take a fetch callback parameter instead.

        Parameters:
        itemFilter - filter to check if an item is shown when user typed some text into the ComboBox
        items - the data items to display
        Returns:
        the in-memory data view instance that provides access to the data bound to the combo box
      • setItems

        public ComboBoxListDataView<T> setItems​(ComboBox.ItemFilter<T> itemFilter,
                                                T... items)
        Sets the data items of this combo box and a filtering function for defining which items are displayed when user types into the combo box.

        Note that defining a custom filter will force the component to make server roundtrips to handle the filtering. Otherwise it can handle filtering in the client-side, if the size of the data set is less than the pageSize.

        Setting the items resets the combo box's value to null.

        The returned data view object can be used for further access to combo box items, or later on fetched with getListDataView(). For using lazy data loading, use one of the setItems methods which take a fetch callback parameter instead.

        Parameters:
        itemFilter - filter to check if an item is shown when user typed some text into the ComboBox
        items - the data items to display
        Returns:
        the in-memory data view instance that provides access to the data bound to the combo box
      • setItems

        public ComboBoxDataView<T> setItems​(InMemoryDataProvider<T> inMemoryDataProvider,
                                            SerializableFunction<String,​SerializablePredicate<T>> filterConverter)
        Sets an in-memory data provider for the combo box to use, taking into account both in-memory filtering from data provider and combo box's text filter.

        Text filter is transformed into a predicate filter through the given filter converter. Example of filter converter which produces the Person's name predicate: (String nameFilter) -> person -> person.getName().equalsIgnoreCase (nameFilter);

        Filtering will be handled in the client-side if the size of the data set is less than the page size. To force client-side filtering with a larger data set (at the cost of increased network traffic), you can increase the page size with setPageSize(int).

        Note! Using a ListDataProvider instead of a InMemoryDataProvider is recommended to get access to ListDataView API by using setItems(ListDataProvider).

        Parameters:
        inMemoryDataProvider - InMemoryDataProvider to use, not null
        filterConverter - a function which converts a component's internal filter into a predicate applied to the data provider
        Returns:
        DataView providing information on the data
      • setItemsWithFilterConverter

        public <C> ComboBoxLazyDataView<T> setItemsWithFilterConverter​(CallbackDataProvider.FetchCallback<T,​C> fetchCallback,
                                                                       SerializableFunction<String,​C> filterConverter)
        Supply items lazily with a callback from a backend, using custom filter type. The combo box will automatically fetch more items and adjust its size until the backend runs out of items. Usage example:

        comboBox.setItemsWithFilterConverter( query -> orderService.getOrdersByCount(query.getFilter(), query.getOffset, query.getLimit()), orderCountStr -> Integer.parseInt(orderCountStr)); Note: Validations for orderCountStr are omitted for briefness.

        Combo box's client-side filter typed by the user is transformed into a callback's filter through the given filter converter.

        The returned data view object can be used for further configuration, or later on fetched with getLazyDataView(). For using in-memory data, like Collection, use setItems(Collection) instead.

        Type Parameters:
        C - filter type used by a callback
        Parameters:
        fetchCallback - function that returns a stream of items from the backend based on the offset, limit and a object filter
        filterConverter - a function which converts a combo box's filter-string typed by the user into a callback's object filter
        Returns:
        ComboBoxLazyDataView instance for further configuration
      • setItemsWithFilterConverter

        public <C> ComboBoxLazyDataView<T> setItemsWithFilterConverter​(CallbackDataProvider.FetchCallback<T,​C> fetchCallback,
                                                                       CallbackDataProvider.CountCallback<T,​C> countCallback,
                                                                       SerializableFunction<String,​C> filterConverter)
        Supply items lazily with callbacks: the first one fetches the items based on offset, limit and an optional filter, the second provides the exact count of items in the backend. Use this only in case getting the count is cheap and the user benefits from the component showing immediately the exact size. Usage example:

        comboBox.setItemsWithFilterConverter( query -> orderService.getOrdersByCount(query.getFilter(), query.getOffset, query.getLimit()), query -> orderService.getSize(query.getFilter()), orderCountStr -> Integer.parseInt(orderCountStr)); Note: Validations for orderCountStr are omitted for briefness.

        Combo box's client-side filter typed by the user is transformed into a custom filter type through the given filter converter.

        The returned data view object can be used for further configuration, or later on fetched with getLazyDataView(). For using in-memory data, like Collection, use setItems(Collection) instead.

        Type Parameters:
        C - filter type used by a callbacks
        Parameters:
        fetchCallback - function that returns a stream of items from the backend based on the offset, limit and a object filter
        filterConverter - a function which converts a combo box's filter-string typed by the user into a callback's object filter
        Returns:
        ComboBoxLazyDataView instance for further configuration
      • setDataProvider

        @Deprecated
        public <C> void setDataProvider​(DataProvider<T,​C> dataProvider,
                                        SerializableFunction<String,​C> filterConverter)
        Deprecated.
        use instead one of the setItems methods which provide access to either ComboBoxListDataView or ComboBoxLazyDataView

        ComboBox triggers filtering queries based on the strings users type into the field. For this reason you need to provide the second parameter, a function which converts the filter-string typed by the user into filter-type used by your data provider. If your data provider already supports String as the filter-type, it can be used without a converter function via setDataProvider(DataProvider).

        Using this method provides the same result as using a data provider wrapped with DataProvider.withConvertedFilter(SerializableFunction).

        Changing the combo box's data provider resets its current value to null.

      • onAttach

        protected void onAttach​(AttachEvent attachEvent)
        Description copied from class: Component
        Called when the component is attached to a UI.

        The default implementation does nothing.

        This method is invoked before the AttachEvent is fired for the component.

        Overrides:
        onAttach in class Component
        Parameters:
        attachEvent - the attach event
      • onDetach

        protected void onDetach​(DetachEvent detachEvent)
        Description copied from class: Component
        Called when the component is detached from a UI.

        The default implementation does nothing.

        This method is invoked before the DetachEvent is fired for the component.

        Overrides:
        onDetach in class Component
        Parameters:
        detachEvent - the detach event
      • setDataProvider

        @Deprecated
        public void setDataProvider​(ListDataProvider<T> listDataProvider)
        Deprecated.
        use instead one of the setItems methods which provide access to ComboBoxListDataView
        Sets a list data provider as the data provider of this combo box.

        Filtering will use a case insensitive match to show all items where the filter text is a substring of the label displayed for that item, which you can configure with setItemLabelGenerator(ItemLabelGenerator).

        Filtering will be handled in the client-side if the size of the data set is less than the page size. To force client-side filtering with a larger data set (at the cost of increased network traffic), you can increase the page size with setPageSize(int).

        Changing the combo box's data provider resets its current value to null.

        Parameters:
        listDataProvider - the list data provider to use, not null
      • setDataProvider

        @Deprecated
        public void setDataProvider​(ComboBox.ItemFilter<T> itemFilter,
                                    ListDataProvider<T> listDataProvider)
        Deprecated.
        Sets a list data provider with an item filter as the data provider of this combo box. The item filter is used to compare each item to the filter text entered by the user.

        Note that defining a custom filter will force the component to make server roundtrips to handle the filtering. Otherwise it can handle filtering in the client-side, if the size of the data set is less than the pageSize.

        Changing the combo box's data provider resets its current value to null.

        Parameters:
        itemFilter - filter to check if an item is shown when user typed some text into the ComboBox
        listDataProvider - the list data provider to use, not null
      • setItems

        public ComboBoxListDataView<T> setItems​(ComboBox.ItemFilter<T> itemFilter,
                                                ListDataProvider<T> listDataProvider)
        Sets a ListDataProvider for this combo box and a filtering function for defining which items are displayed when user types into the combo box.

        Note that defining a custom filter will force the component to make server roundtrips to handle the filtering. Otherwise it can handle filtering in the client-side, if the size of the data set is less than the pageSize.

        Setting the items resets the combo box's value to null.

        The returned data view object can be used for further access to combo box items, or later on fetched with getListDataView(). For using lazy data loading, use one of the setItems methods which take a fetch callback parameter instead.

        Parameters:
        itemFilter - filter to check if an item is shown when user typed some text into the ComboBox.
        listDataProvider - ListDataProvider providing items to the component.
        Returns:
        the in-memory data view instance that provides access to the data bound to the combo box
      • getDataProvider

        public DataProvider<T,​?> getDataProvider()
        Gets the data provider used by this ComboBox.
        Returns:
        the data provider used by this ComboBox
      • setItemLabelGenerator

        public void setItemLabelGenerator​(ItemLabelGenerator<T> itemLabelGenerator)
        Sets the item label generator that is used to produce the strings shown in the combo box for each item. By default, String.valueOf(Object) is used.

        When the setRenderer(Renderer) is used, the ItemLabelGenerator is only used to show the selected item label.

        Parameters:
        itemLabelGenerator - the item label provider to use, not null
      • getItemLabelGenerator

        public ItemLabelGenerator<T> getItemLabelGenerator()
        Gets the item label generator that is used to produce the strings shown in the combo box for each item.
        Returns:
        the item label generator used, not null
      • setPageSize

        public void setPageSize​(int pageSize)
        Sets the page size, which is the number of items requested at a time from the data provider. This does not guarantee a maximum query size to the backend; when the overlay has room to render more new items than the page size, multiple "pages" will be requested at once.

        The page size is also the largest number of items that can support client-side filtering. If you provide more items than the page size, the component has to fall back to server-side filtering.

        Setting the page size after the ComboBox has been rendered effectively resets the component, and the current page(s) and sent over again.

        The default page size is 50.

        Parameters:
        pageSize - the maximum number of items sent per request, should be greater than zero
      • getPageSize

        public int getPageSize()
        Gets the page size, which is the number of items fetched at a time from the data provider.

        The page size is also the largest number of items that can support client-side filtering. If you provide more items than the page size, the component has to fall back to server-side filtering.

        The default page size is 50.

        Returns:
        the maximum number of items sent per request
      • setOpened

        public void setOpened​(boolean opened)
        Description copied from class: GeneratedVaadinComboBox

        Description copied from corresponding location in WebComponent:

        True if the dropdown is open, false otherwise.

        Overrides:
        setOpened in class GeneratedVaadinComboBox<ComboBox<T>,​T>
        Parameters:
        opened - the boolean value to set
      • isOpened

        public boolean isOpened()
        Gets the states of the drop-down.
        Returns:
        true if the drop-down is opened, false otherwise
      • isInvalid

        public boolean isInvalid()
        Gets the validity of the combobox output.

        return true, if the value is invalid.

        Specified by:
        isInvalid in interface HasValidation
        Returns:
        the validity property from the component
      • getErrorMessage

        public String getErrorMessage()
        Gets the current error message from the combobox.
        Specified by:
        getErrorMessage in interface HasValidation
        Returns:
        the current error message
      • setAllowCustomValue

        public void setAllowCustomValue​(boolean allowCustomValue)
        Enables or disables the component firing events for custom string input.

        When enabled, a GeneratedVaadinComboBox.CustomValueSetEvent will be fired when the user inputs a string value that does not match any existing items and commits it eg. by blurring or pressing the enter-key.

        Note that ComboBox doesn't do anything with the custom value string automatically. Use the addCustomValueSetListener(ComponentEventListener) method to determine how the custom value should be handled. For example, when the ComboBox has String as the value type, you can add a listener which sets the custom string as the value of the ComboBox with setValue(Object).

        Setting to true also allows an unfocused ComboBox to display a string that doesn't match any of its items nor its current value, unless this is explicitly handled with addCustomValueSetListener(ComponentEventListener). When set to false, an unfocused ComboBox will always display the label of the currently selected item.

        Overrides:
        setAllowCustomValue in class GeneratedVaadinComboBox<ComboBox<T>,​T>
        Parameters:
        allowCustomValue - true to enable custom value set events, false to disable them
        See Also:
        addCustomValueSetListener(ComponentEventListener)
      • setAutoOpen

        public void setAutoOpen​(boolean autoOpen)
        Enables or disables the dropdown opening automatically. If false the dropdown is only opened when clicking the toggle button or pressing Up or Down arrow keys.
        Parameters:
        autoOpen - false to prevent the dropdown from opening automatically
      • isAutoOpen

        public boolean isAutoOpen()
        Gets whether dropdown will open automatically or not.
        Returns:
      • setAutofocus

        public void setAutofocus​(boolean autofocus)
        Set the combobox to be input focused when the page loads.
        Overrides:
        setAutofocus in class GeneratedVaadinComboBox<ComboBox<T>,​T>
        Parameters:
        autofocus - the boolean value to set
      • isAutofocus

        public boolean isAutofocus()
        Get the state for the auto-focus property of the combobox.

        This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.

        Returns:
        the autofocus property from the combobox
      • setPreventInvalidInput

        public void setPreventInvalidInput​(boolean preventInvalidInput)
        Description copied from class: GeneratedVaadinComboBox

        Description copied from corresponding location in WebComponent:

        Set to true to prevent the user from entering invalid input.

        Overrides:
        setPreventInvalidInput in class GeneratedVaadinComboBox<ComboBox<T>,​T>
        Parameters:
        preventInvalidInput - the boolean value to set
      • isPreventInvalidInput

        public boolean isPreventInvalidInput()
        Determines whether preventing the user from inputing invalid value.

        This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.

        Returns:
        the preventInvalidInput property of the combobox
      • setRequired

        public void setRequired​(boolean required)
        Description copied from class: GeneratedVaadinComboBox

        Description copied from corresponding location in WebComponent:

        Set to true to mark the input as required.

        Overrides:
        setRequired in class GeneratedVaadinComboBox<ComboBox<T>,​T>
        Parameters:
        required - the boolean value to set
      • isRequired

        public boolean isRequired()
        Determines whether the combobox is marked as input required.

        This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.

        Returns:
        true if the input is required, false otherwise
      • getLabel

        public String getLabel()
        Gets the label of the combobox.
        Specified by:
        getLabel in interface HasLabel
        Returns:
        the label property of the combobox
      • getPlaceholder

        public String getPlaceholder()
        Gets the placeholder of the combobox.
        Returns:
        the placeholder property of the combobox
      • getPattern

        public String getPattern()
        Gets the valid input pattern
        Returns:
        the pattern property of the combobox
      • addCustomValueSetListener

        public Registration addCustomValueSetListener​(ComponentEventListener<GeneratedVaadinComboBox.CustomValueSetEvent<ComboBox<T>>> listener)
        Adds a listener for the event which is fired when user inputs a string value that does not match any existing items and commits it eg. by blurring or pressing the enter-key.

        Note that ComboBox doesn't do anything with the custom value string automatically. Use this method to determine how the custom value should be handled. For example, when the ComboBox has String as the value type, you can add a listener which sets the custom string as the value of the ComboBox with setValue(Object).

        As a side effect, this makes the ComboBox allow custom values. If you want to disable the firing of custom value set events once the listener is added, please disable it explicitly via the setAllowCustomValue(boolean) method.

        The custom value becomes disallowed automatically once the last custom value set listener is removed.

        Overrides:
        addCustomValueSetListener in class GeneratedVaadinComboBox<ComboBox<T>,​T>
        Parameters:
        listener - the listener to be notified when a new value is filled
        Returns:
        a Registration for removing the event listener
        See Also:
        setAllowCustomValue(boolean)
      • setRequiredIndicatorVisible

        public void setRequiredIndicatorVisible​(boolean requiredIndicatorVisible)
        Description copied from interface: HasValue
        Sets the required indicator visible or not.

        If set visible, it is visually indicated in the user interface.

        The method is intended to be used with Binder which does server-side validation. In case HTML element has its own (client-side) validation it should be disabled when setRequiredIndicatorVisible(true) is called and re-enabled back on setRequiredIndicatorVisible(false). It's responsibility of each component implementation to follow the contract so that the method call doesn't do anything else than show/hide the "required" indication. Usually components provide their own setRequired method which should be called in case the client-side validation is required.

        Specified by:
        setRequiredIndicatorVisible in interface HasValue<AbstractField.ComponentValueChangeEvent<ComboBox<T>,​T>,​T>
        Specified by:
        setRequiredIndicatorVisible in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<ComboBox<T>,​T>,​T>
        Parameters:
        requiredIndicatorVisible - true to make the required indicator visible, false if not
      • setClearButtonVisible

        public void setClearButtonVisible​(boolean clearButtonVisible)
        Allows displaying a clear button in the combo box when a value is selected.

        The clear button is an icon, which can be clicked to set the combo box value to null.

        Overrides:
        setClearButtonVisible in class GeneratedVaadinComboBox<ComboBox<T>,​T>
        Parameters:
        clearButtonVisible - true to display the clear button, false to hide it
      • isClearButtonVisible

        public boolean isClearButtonVisible()
        Gets whether this combo box displays a clear button when a value is selected.
        Returns:
        true if this combo box displays a clear button, false otherwise
        See Also:
        setClearButtonVisible(boolean)
      • setItems

        public ComboBoxLazyDataView<T> setItems​(CallbackDataProvider.FetchCallback<T,​String> fetchCallback)
        Supply items lazily with a callback from a backend. The ComboBox will automatically fetch more items and adjust its size until the backend runs out of items. Usage example without component provided filter:

        comboBox.setItems(query -> orderService.getOrders(query.getOffset(), query.getLimit());

        Since ComboBox supports filtering, it can be fetched via query.getFilter():

        comboBox.setItems(query -> orderService.getOrders(query.getFilter(), query.getOffset(), query.getLimit());

        The returned data view object can be used for further configuration, or later on fetched with getLazyDataView(). For using in-memory data, like Collection, use HasListDataView.setItems(Collection) instead.

        If item filtering by some value type other than String is preferred and backend service is able to fetch and filter items by such type, converter for client side's filter string can be set along with fetch callback. See: setItemsWithFilterConverter(CallbackDataProvider.FetchCallback, SerializableFunction)

        Specified by:
        setItems in interface HasLazyDataView<T,​String,​ComboBoxLazyDataView<T>>
        Parameters:
        fetchCallback - function that returns a stream of items from the backend based on the offset, limit and an optional filter provided by the query object
        Returns:
        ComboBoxLazyDataView instance for further configuration
      • setItems

        public ComboBoxLazyDataView<T> setItems​(CallbackDataProvider.FetchCallback<T,​String> fetchCallback,
                                                CallbackDataProvider.CountCallback<T,​String> countCallback)
        Supply items lazily with callbacks: the first one fetches the items based on offset, limit and an optional filter, the second provides the exact count of items in the backend. Use this only in case getting the count is cheap and the user benefits from the ComboBox showing immediately the exact size. Usage example without component provided filter:

        comboBox.setItems( query -> orderService.getOrders(query.getOffset, query.getLimit()), query -> orderService.getSize());

        Since ComboBox supports filtering, it can be fetched via query.getFilter():

        comboBox.setItems( query -> orderService.getOrders(query.getFilter(), query.getOffset, query.getLimit()), query -> orderService.getSize(query.getFilter()));

        The returned data view object can be used for further configuration, or later on fetched with getLazyDataView(). For using in-memory data, like Collection, use HasListDataView.setItems(Collection) instead.

        If item filtering by some value type other than String is preferred and backend service is able to fetch and filter items by such type, converter for client side's filter string can be set along with fetch callback. See: setItemsWithFilterConverter(CallbackDataProvider.FetchCallback, CallbackDataProvider.CountCallback, SerializableFunction)

        Specified by:
        setItems in interface HasLazyDataView<T,​String,​ComboBoxLazyDataView<T>>
        Parameters:
        fetchCallback - function that returns a stream of items from the back end for a query
        countCallback - function that return the number of items in the back end for a query
        Returns:
        ComboBoxLazyDataView instance for further configuration
      • addThemeVariants

        public void addThemeVariants​(ComboBoxVariant... variants)
        Adds theme variants to the component.
        Parameters:
        variants - theme variants to add
      • removeThemeVariants

        public void removeThemeVariants​(ComboBoxVariant... variants)
        Removes theme variants from the component.
        Parameters:
        variants - theme variants to remove