Class Label

All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasComponents, HasElement, HasEnabled, HasSize, HasStyle, HasText, Serializable

@Tag("label") @Deprecated(since="24.1", forRemoval=true) public class Label extends HtmlContainer
Deprecated, for removal: This API element is subject to removal in a future version.
Use NativeLabel instead, if you need the HTML <label> element, which is normally not needed within a Vaadin Flow application's high-level components. To use a Label that works in the older style of Vaadin 8 or Java Swing, and can be used both to label a component and to display loose text, an alternative is the Label available in Classic Components. This Label component / <label> element is not meant for loose text in the page - it should only be coupled with another component by using the setFor(Component) or by adding them to it with the HasComponents.add(Component...) method, for example if you use Input.
Component for a <label> element, which represents a caption for an item in a user interface.

Clicking on a label automatically transfers the focus to the associated component. This is especially helpful when building forms with Inputs.

For adding texts to the page without linking them to other components, consider using a Span or a Div instead. If the text should be interpreted as HTML, use a Html (but remember to guard against cross-site scripting attacks).

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • Label

      public Label()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Creates a new empty label.
    • Label

      public Label(String text)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Creates a new label with the given text content.
      Parameters:
      text - the text content
  • Method Details

    • setFor

      public void setFor(Component forComponent)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Sets the component that this label describes. The component (or its id) should be defined in case the described component is not an ancestor of the label.

      The provided component must have an id set. This component will still use the old id if the id of the provided component is changed after this method has been called.

      Parameters:
      forComponent - the component that this label describes, not null , must have an id
      Throws:
      IllegalArgumentException - if the provided component has no id
    • setFor

      public void setFor(String forId)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Sets the id of the component that this label describes. The id should be defined in case the described component is not an ancestor of the label.
      Parameters:
      forId - the id of the described component, or null if there is no value
    • getFor

      public Optional<String> getFor()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Gets the id of the component that this label describes.
      Returns:
      an optional id of the described component, or an empty optional if the attribute has not been set
      See Also:
    • onAttach

      protected void onAttach(AttachEvent attachEvent)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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