Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

Using Vaadin Mixin Interfaces

A mixin refers to a defined amount of functionality that can be added to a class. Traditionally, Java did not support this kind of multiple inheritance, but since Java 8 interfaces can also include default methods, which allows them to work as mixins.

Vaadin Flow uses the mixin concept to provide common APIs and default behavior for sets of functionalities found in most Web Components.

The most important predefined mixins are provided by the HasSize, HasComponents and HasStyle interfaces. You can use these interfaces to add typical functions to your Java components.

HasSize Interface

If your component implements the HasSize interface, you can set the size of the component using the setWidth(String) and setHeight(String) methods.

Methods available in the HasSize interface:

  • void setWidth(String width)

  • String getWidth()

  • void setHeight(String height)

  • String getHeight()

  • void setSizeFull()

  • void setSizeUndefined()

HasComponents Interface

If your component implements the HasComponents interface, you can add and remove child components to and from it.

Methods available in the HasComponents interface:

  • void add(Component…​ components)

  • void remove(Component…​ components)

  • void removeAll()

HasStyle Interface

Components that implement the HasStyle interface can have a class attribute and support inline styles.

Methods available in the HasStyle interface:

  • void addClassName(String className)

  • boolean removeClassName(String className)

  • void setClassName(String className)

  • String getClassName()

  • ClassList getClassNames()

  • void setClassName(String className, boolean set)

  • boolean hasClassName(String className)

  • Style getStyle()

  • void addClassNames(String…​ classNames)

  • void removeClassNames(String…​ classNames)`

Using Mixin Interfaces

Example: Creating a custom Tooltip component that implements the HasComponents and HasStyle interfaces.

public class Tooltip extends Component
        implements HasComponents, HasStyle {

class Tooltip extends PolymerElement {
    static get template() {
        return html`
            <div part="content" theme="dark">
  • A component that HasComponents needs to extend from a tag that supports having child components. The slot tag is used in Web Components to define where child components should be put.

When you implement the HasComponents interface, adding child components to the parent component is allowed automatically.

Example: Adding new H5 and Paragraph child components to the Tooltip parent component.

Tooltip tooltip = new Tooltip();

tooltip.add(new H5("Tooltip"));
tooltip.add(new Paragraph("I am a paragraph"));

Other Useful Mixin Interfaces

Vaadin Flow provides many additional useful mixin interfaces:

  • HasEnabled: Generic interface for components and other UI objects that can be enabled or disabled.

  • HasElement: Marker interface for any class that is based on an Element.

  • HasDataProvider<T>: Generic interface for listing components that use a data provider to display data.

  • HasValidation: Generic interface that supports input validation.

  • HasItems: Mixin interface for components that display a collection of items.

  • HasOrderedComponents: Generic interface that supports ordered child components, with an index for the layout.

  • HasText: Generic interface that supports text content.

  • Focusable<T>: Interface that provides methods to gain and lose focus.

Advantages of Using Mixin Interfaces

Using Vaadin mixins is a best practice because their code and functionality has been throughly checked and tested by Vaadin.

Mixins also keep your code clean and simple. For example, compare setting component width:

  • Without mixins: getElement().getStyle().set("width", "300px").

  • After implementing the HasSize interface: setWidth("300px").