Class NativeLabel

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
com.vaadin.flow.component.HtmlContainer
com.vaadin.flow.component.html.NativeLabel
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasText, Serializable

@Tag("label") public class NativeLabel extends com.vaadin.flow.component.HtmlContainer
Component for a <label> element, which represents a caption for an input field in a user interface. Note that Label components are not meant for loose text in the page - they should be coupled with another component by using the setFor(Component) or by adding them to it with the HasComponents.add(Component...) method. 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:
24.1
Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasText

    com.vaadin.flow.component.HasText.WhiteSpace
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new empty label.
    Creates a new label with the given text content.
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the id of the component that this label describes.
    void
    setFor(com.vaadin.flow.component.Component forComponent)
    Sets the component that this label describes.
    void
    setFor(String forId)
    Sets the id of the component that this label describes.

    Methods inherited from class com.vaadin.flow.component.HtmlComponent

    getTitle, setTitle

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

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

    addAttachListener

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

    addDetachListener

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

    add, add, add, addComponentAsFirst, addComponentAtIndex, remove, remove, removeAll

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

    getElement

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

    isEnabled, setEnabled

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

    getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull

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

    addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName

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

    getText, getWhiteSpace, setText, setWhiteSpace
  • Constructor Details

    • NativeLabel

      public NativeLabel()
      Creates a new empty label.
    • NativeLabel

      public NativeLabel(String text)
      Creates a new label with the given text content.
      Parameters:
      text - the text content
  • Method Details

    • setFor

      public void setFor(com.vaadin.flow.component.Component forComponent)
      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)
      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()
      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: