Class CssLayout

All Implemented Interfaces:
LayoutEvents.LayoutClickNotifier, ClientConnector, FAbstractClientConnector, FSizeable, Sizeable, ComponentContainer, FAbstractComponent, HasComponents, HasComponents.ComponentAttachDetachNotifier, Layout, com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.HasComponentsOfType<com.vaadin.flow.component.Component>, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasLabel, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.shared.HasTooltip, Serializable, Iterable<com.vaadin.flow.component.Component>

@Tag("div") public class CssLayout extends AbstractLayout implements LayoutEvents.LayoutClickNotifier
CssLayout is a layout component that can be used in browser environment only. It simply renders components and their captions into a same div element. Component layout can then be adjusted with css.

In comparison to HorizontalLayout and VerticalLayout

  • rather similar server side api
  • no spacing, alignment or expand ratios
  • much simpler DOM that can be styled by skilled web developer
  • no abstraction of browser differences (developer must ensure that the result works properly on each browser)
  • different kind of handling for relative sizes (that are set from server side) (*)
  • noticeably faster rendering time in some situations as we rely more on the browser's rendering engine.

By extending CssLayout one can also inject some css rules straight to child components using getCss(Component).

(*) Relative sizes (set from server side) are treated bit differently than in other layouts in Vaadin. In cssLayout the size is calculated relatively to CSS layouts content area which is pretty much as in html and css. In other layouts the size of component is calculated relatively to the "slot" given by layout.

Also note that client side framework in Vaadin modifies inline style properties width and height. This happens on each update to component. If one wants to set component sizes with CSS, component must have undefined size on server side (which is not the default for all components) and the size must be defined with class styles - not by directly injecting width and height.

See Also:
  • Field Details

    • components

      protected LinkedList<com.vaadin.flow.component.Component> components
      Custom layout slots containing the components.
  • Constructor Details

    • CssLayout

      public CssLayout()
      Constructs an empty CssLayout.
    • CssLayout

      public CssLayout(com.vaadin.flow.component.Component... children)
      Constructs a CssLayout with the given components in the given order.
      Parameters:
      children - Components to add to the container.
      See Also:
  • Method Details

    • setLabelComponent

      public void setLabelComponent(com.vaadin.flow.component.Component labelComponent)
      Description copied from interface: FAbstractComponent
      Sets the component's label component. Flow components that support caption component usually overrides this.
      Specified by:
      setLabelComponent in interface FAbstractComponent
      Parameters:
      labelComponent - the new label component for the component.
    • addComponent

      public void addComponent(com.vaadin.flow.component.Component c)
      Add a component into this container. The component is added to the right or below the previous component.
      Specified by:
      addComponent in interface ComponentContainer
      Overrides:
      addComponent in class AbstractComponentContainer
      Parameters:
      c - the component to be added.
    • addComponentAsFirst

      public void addComponentAsFirst(com.vaadin.flow.component.Component c)
      Adds a component into this container. The component is added to the left or on top of the other components.
      Specified by:
      addComponentAsFirst in interface com.vaadin.flow.component.HasComponentsOfType<com.vaadin.flow.component.Component>
      Parameters:
      c - the component to be added.
    • addComponent

      public void addComponent(com.vaadin.flow.component.Component c, int index)
      Adds a component into indexed position in this container.
      Parameters:
      c - the component to be added.
      index - the index of the component position. The components currently in and after the position are shifted forwards.
    • removeComponent

      public void removeComponent(com.vaadin.flow.component.Component c)
      Removes the component from this container.
      Specified by:
      removeComponent in interface ComponentContainer
      Overrides:
      removeComponent in class AbstractComponentContainer
      Parameters:
      c - the component to be removed.
    • iterator

      public Iterator<com.vaadin.flow.component.Component> iterator()
      Gets the component container iterator for going trough all the components in the container.
      Specified by:
      iterator in interface HasComponents
      Specified by:
      iterator in interface Iterable<com.vaadin.flow.component.Component>
      Overrides:
      iterator in class AbstractComponentContainer
      Returns:
      the Iterator of the components inside the container.
    • getComponentCount

      public int getComponentCount()
      Gets the number of contained components. Consistent with the iterator returned by iterator().
      Specified by:
      getComponentCount in interface ComponentContainer
      Specified by:
      getComponentCount in interface com.vaadin.flow.component.HasComponentsOfType<com.vaadin.flow.component.Component>
      Returns:
      the number of contained components
    • beforeClientResponse

      public void beforeClientResponse(boolean initial)
      Description copied from interface: ClientConnector
      Called before the response is written to client side. Gives the legacy connector an opportunity to set computed/dynamic state values.

      This is implemented for the legacy components to keep consistent behavior and e.g. legaze size calculations working.

      NOTE: the parameter initial is false when the component is attached again to a new UI when PreserveOnRefresh is used.

      Specified by:
      beforeClientResponse in interface ClientConnector
      Specified by:
      beforeClientResponse in interface FAbstractClientConnector
      Specified by:
      beforeClientResponse in interface FAbstractComponent
      Overrides:
      beforeClientResponse in class AbstractComponent
      Parameters:
      initial - true if the client-side connector will be created and initialized after this method has been invoked. false if there is already an initialized client-side connector.
    • getCss

      protected String getCss(com.vaadin.flow.component.Component c)
      Returns styles to be applied to given component. Override this method to inject custom style rules to components.

      Note that styles are injected over previous styles before actual child rendering. Previous styles are not cleared, but overridden.

      Note that one most often achieves better code style, by separating styling to theme (with custom theme and FAbstractComponent.addStyleName(String). With own custom styles it is also very easy to break browser compatibility.

      Parameters:
      c - the component
      Returns:
      CSS rules to be applied to a component
    • replaceComponent

      public void replaceComponent(com.vaadin.flow.component.Component oldComponent, com.vaadin.flow.component.Component newComponent)
      Description copied from interface: ComponentContainer
      Replaces the component in the container with another one without changing position.

      This method replaces component with another one is such 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 the both component are already in the container, their positions are swapped. Component attach and detach events should be taken care as with add and remove.

      Specified by:
      replaceComponent in interface ComponentContainer
      Parameters:
      oldComponent - the old component that will be replaced.
      newComponent - the new component to be replaced.
    • addLayoutClickListener

      public com.vaadin.flow.shared.Registration addLayoutClickListener(LayoutEvents.LayoutClickListener listener)
      Description copied from interface: LayoutEvents.LayoutClickNotifier
      Add a click listener to the layout. The listener is called whenever the user clicks inside the layout. An event is also triggered when the click targets a component inside a nested layout or Panel, provided the targeted component does not prevent the click event from propagating. A caption is not considered part of a component.

      The child component that was clicked is included in the LayoutEvents.LayoutClickEvent.

      Specified by:
      addLayoutClickListener in interface LayoutEvents.LayoutClickNotifier
      Parameters:
      listener - The listener to add
      Returns:
      a registration object for removing the listener
      See Also:
      • Registration
    • removeLayoutClickListener

      public void removeLayoutClickListener(LayoutEvents.LayoutClickListener listener)
    • getComponentIndex

      public int getComponentIndex(com.vaadin.flow.component.Component component)
      Returns the index of the given component.
      Parameters:
      component - The component to look up.
      Returns:
      The index of the component or -1 if the component is not a child.
    • getComponent

      public com.vaadin.flow.component.Component getComponent(int index) throws IndexOutOfBoundsException
      Returns the component at the given position.
      Parameters:
      index - The position of the component.
      Returns:
      The component at the given index.
      Throws:
      IndexOutOfBoundsException - If the index is out of range.