Creating a Component Container

In this section we demonstrate how to create a Component container.

A component container is a component to which you can add other components. A container is typically created through a generic public API.

Example: Simple component container.

Java

  1. @Tag("div")
  2. public class MyComponentContainer extends Component
  3. implements HasComponents {
  4. }
  • The HasComponents interface provides add(Component…​) and remove(Component…​) methods to handle attaching component elements to the MyComponentContainer root element ( <div> in this example).

Implementing a Custom Add Method

If necessary, you can implement your own add method. For example if you need a different kind of API or have a complex internal element hierarchy.

Example: Implementing a custom method to add components to a container.

Java

  1. @Tag("div")
  2. public class MyComponentContainer extends Component {
  3. public void add(Component child) {
  4. getElement().appendChild(child.getElement());
  5. }
  6. }

Attaching Child Components to the DOM

When a child component is added to a container component, the container must attach the child to the DOM. This is the only absolute requirement for a container component.

In the previous example, the child element attaches to the root element (like HasComponents does). As an alternative, you can wrap each child in a wrapper element or use a more complex element hierarchy, where necessary.

Example: Wrapping a child component in an element wrapper.

Java

  1. @Tag("div")
  2. public class MyComponentContainer extends Component {
  3. public void add(Component child) {
  4. Element childWrapper = ElementFactory
  5. .createDiv();
  6. childWrapper.appendChild(child.getElement());
  7. getElement().appendChild(childWrapper);
  8. }
  9. }

Using Component Hierarchy Methods

Component hierarchy methods, such as getChildren and getParent, work automatically for container components, because they are implemented based on the element hierarchy. Theses methods also work if you add wrapper elements in between.

You can add a similar method to remove components.

Example: Using the removeFromParent method to detach a component.

Java

  1. public void remove(Component child) {
  2. Element wrapper = child.getElement().getParent();
  3. wrapper.removeFromParent();
  4. }
Note
You cannot assume that a component will always be removed when using the remove method. A child element can be detached manually through the Element API (for example, using the Element.removeFromParent() method) or by adding it to another component (for example, the Element.appendChild method moves the element from the old parent, if it is still attached).
Tip
If you need to know when a child component is removed, add a detach listener to it using the Component.addDetachListener() method.

Enabling and Disabling Container Components

When you set a container component as disabled, all child components are automatically also set as disabled, and updates from the client to the server are blocked.

Components that implement the HasEnabled interface are updated accordingly to reflect the disabled state in the UI (which usually means setting the disabled attribute).

If your container includes elements or components that do not implement the HasEnabled interface, you can still visually update them to reflect the disabled state in the UI, by overriding the onEnabledStateChanged method.

Example: Setting a component as disabled by overriding he onEnabledStateChanged method.

Java

  1. @Override
  2. public void onEnabledStateChanged(boolean enabled) {
  3. super.onEnabledStateChanged(enabled);
  4. if (enabled) {
  5. childElement.removeAttribute("disabled");
  6. } else {
  7. childElement.setAttribute("disabled", true);
  8. }
  9. }
  • You only need to override the onEnabledStateChanged method to update the visual aspect of the element. When the container is disabled, communication from the client to the server is blocked, regardless of whether or not you override the method.

  • It is important to call super.onEnabledStateChanged(enabled) when overriding, because this is common logic and relevant to all components regarding the enabled state.

  • The onEnabledStateChanged method is called every time the enabled state changes, whether by direct calls to setEnabled, by calling setEnabled on a parent container, or by attaching or detaching the component to a disabled container.

See Component Enabled State for more.