Interface HasComponentsOfType<T extends Component>

Type Parameters:
T - the type of child components accepted by this container
All Superinterfaces:
HasElement, HasEnabled, Serializable
All Known Subinterfaces:
FlexComponent, HasComponents, HasItemComponents<T>, HasItemsAndComponents<T>, HasOrderedComponents
All Known Implementing Classes:
Abbr, AccordionPanel, Anchor, Article, Aside, Board, Card, CheckboxGroup, Code, ConfirmDialog, DescriptionList, DescriptionList.Description, DescriptionList.Term, Details, Dialog, Dialog.DialogFooter, Dialog.DialogHeader, Div, Emphasis, FieldSet, FieldSet.Legend, FlexLayout, Footer, FormLayout, FormLayout.FormItem, FormLayout.FormRow, GridMenuItem, H1, H2, H3, H4, H5, H6, Header, HorizontalLayout, HtmlContainer, HtmlObject, Image, ListBox, ListBoxBase, ListItem, Main, MenuItem, MenuItemBase, MultiSelectListBox, NativeButton, NativeDetails.Summary, NativeLabel, NativeTable, NativeTableBody, NativeTableCaption, NativeTableCell, NativeTableFooter, NativeTableHeader, NativeTableHeaderCell, NativeTableRow, Nav, Notification, OrderedList, Paragraph, Popover, Pre, RouterLink, Row, Section, Select, Span, Tab, UI, UnorderedList, VerticalLayout, WebComponentUI

public interface HasComponentsOfType<T extends Component> extends HasElement, HasEnabled
A component to which the user can add and remove child components of a specific type. This is the typed counterpart of HasComponents; use it when the container should only accept a restricted set of child component types (for example a breadcrumb trail that only accepts breadcrumb items).

HasComponents is equivalent to HasComponentsOfType<Component> and is the right choice for containers that accept any component. Implement HasComponentsOfType<MyItem> instead when the API should statically prevent callers from adding unrelated components.

The default implementations assume that children are attached to HasElement.getElement(). Override all methods in this interface if the components should be added to some other element.

Author:
Vaadin Ltd
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    add(Collection<T> components)
    Adds the given components as children of this component.
    default void
    add(T... components)
    Adds the given components as children of this component.
    default void
    Adds the given component as the first child of this component.
    default void
    addComponentAtIndex(int index, T component)
    Adds the given component as a child of this component at the specific index.
    default <V extends @Nullable Object, S extends Signal<V>>
    void
    bindChildren(Signal<List<S>> list, SerializableFunction<S,T> childFactory)
    Binds a list Signal to this component using a child component factory.
    default Stream<Component>
    Gets the children components of this component.
    default T
    getComponentAt(int index)
    Returns the component at the given position.
    default int
    Gets the number of children components.
    default int
    indexOf(T component)
    Returns the index of the given component.
    default void
    remove(Collection<T> components)
    Removes the given child components from this component.
    default void
    remove(T... components)
    Removes the given child components from this component.
    default void
    Removes all contents from this component, including child components, text content as well as child elements that have been added directly to this component using the Element API.
    default void
    replace(T oldComponent, T newComponent)
    Replaces the component in the container with another one without changing position.

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasEnabled

    bindEnabled, isEnabled, setEnabled
  • Method Details

    • add

      default void add(T... components)
      Adds the given components as children of this component.

      In case any of the specified components has already been added to another parent, it will be removed from there and added to this one.

      Parameters:
      components - the components to add
    • add

      default void add(Collection<T> components)
      Adds the given components as children of this component.

      In case any of the specified components has already been added to another parent, it will be removed from there and added to this one.

      Parameters:
      components - the components to add
    • remove

      default void remove(T... components)
      Removes the given child components from this component.
      Parameters:
      components - the components to remove
      Throws:
      IllegalArgumentException - if there is a component whose non null parent is not this component
    • remove

      default void remove(Collection<T> components)
      Removes the given child components from this component.
      Parameters:
      components - the components to remove
      Throws:
      IllegalArgumentException - if there is a component whose non null parent is not this component
    • removeAll

      default void removeAll()
      Removes all contents from this component, including child components, text content as well as child elements that have been added directly to this component using the Element API. It also removes the children added only at the client-side.
    • addComponentAtIndex

      default void addComponentAtIndex(int index, T component)
      Adds the given component as a child of this component at the specific index.

      In case the specified component has already been added to another parent, it will be removed from there and added to this one.

      Parameters:
      index - the index, where the component will be added. The index must be non-negative and may not exceed the children count
      component - the component to add, value should not be null
    • addComponentAsFirst

      default void addComponentAsFirst(T component)
      Adds the given component as the first child of this component.

      In case the specified component has already been added to another parent, it will be removed from there and added to this one.

      Parameters:
      component - the component to add, value should not be null
    • bindChildren

      default <V extends @Nullable Object, S extends Signal<V>> void bindChildren(Signal<List<S>> list, SerializableFunction<S,T> childFactory)
      Binds a list Signal to this component using a child component factory. Each item Signal in the list corresponds to a child component within this component.

      This component is automatically updated to reflect the structure of the list. Changes to the list, such as additions, removals, or reordering, will update this component's children accordingly.

      This component must not contain any children in the default slot (i.e. without a slot attribute) that are not part of the list. If this component has existing default-slot children when this method is called, or if it contains unrelated default-slot children after the list changes, an IllegalStateException will be thrown. Named-slot children are allowed and can be added or removed freely while the binding is active.

      New child components are created using the provided childFactory function. This function takes a Signal from the list and returns a corresponding child component. It shouldn't return null. The Signal can be further bound to the returned component as needed. Note that childFactory is run inside a Effect, and therefore Signal.get() calls makes effect re-run automatically on signal value change.

      Example of usage:

       SharedListSignal<String> taskList = new SharedListSignal<>(String.class);
      
       UnorderedList component = new UnorderedList();
      
       component.bindChildren(taskList, ListItem::new);
       

      Note: The default implementation adds children directly to the component’s element using the Element API and does not invoke add(Component...). Components that override add or manage children indirectly must override this method to provide a suitable implementation or explicitly disable it.

      Type Parameters:
      V - the value type of the Signals in the list
      S - the type of the Signals in the list
      Parameters:
      list - list signal to bind to the parent, must not be null
      childFactory - factory to create new component, must not be null
      Throws:
      IllegalStateException - thrown if this component has default-slot children, or if the child factory produces elements with a slot attribute
      BindingActiveException - thrown if a binding for children already exists
    • replace

      default void replace(T oldComponent, T newComponent)
      Replaces the component in the container with another one without changing position. This method replaces a component with another one is such a way that the new component overtakes the position of the old component. If the old component is not in the container, the new component is added to the container. If both components are already in the container, their positions are swapped. Component attach and detach events should be taken care as with add and remove.
      Parameters:
      oldComponent - the old component that will be replaced. Can be null, which will make the newComponent to be added to the layout without replacing any other
      newComponent - the new component to be replaced. Can be null, which will make the oldComponent to be removed from the layout without adding any other
    • indexOf

      default int indexOf(T component)
      Returns the index of the given component.
      Parameters:
      component - the component to look up, cannot be null
      Returns:
      the index of the component or -1 if the component is not a child
    • getComponentCount

      default int getComponentCount()
      Gets the number of children components.
      Returns:
      the number of components
    • getComponentAt

      default T getComponentAt(int index)
      Returns the component at the given position.

      The returned value is cast to T. Only components added through this interface's typed add methods are guaranteed to match T at runtime — direct Element-level manipulation can still introduce children of other types and will produce ClassCastException here.

      Parameters:
      index - the position of the component must be greater than or equals to 0 and less than the number of children components
      Returns:
      The component at the given index
      Throws:
      IllegalArgumentException - if the index is less than 0 or greater than or equals to the number of children components
      See Also:
    • getChildren

      default Stream<Component> getChildren()
      Gets the children components of this component.

      The return type is Stream<Component> rather than Stream<T> so that this method matches Component.getChildren() and can be inherited by classes that extend Component. Although this container only accepts children of type T through its typed add methods, callers may receive untyped Component instances here; use getComponentAt(int) for a typed lookup.

      Returns:
      the children components of this component
      See Also: