Class Label

All Implemented Interfaces:
Property<String>, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer, ClientConnector, FAbstractClientConnector, FSizeable, Sizeable, FAbstractComponent, com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, 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

@Tag("div") @CssImport("./label.css") @CssImport("@vaadin/vaadin-lumo-styles/src/props/icons.css") public class Label extends AbstractComponent implements Property<String>, Property.Viewer, Property.ValueChangeListener, Property.ValueChangeNotifier
Legacy version of Label component for showing non-editable short texts.

The label content can be set to the modes specified by ContentMode. If content mode is set to HTML, any HTML content is allowed.

Author:
Vaadin Ltd.
See Also:
  • Constructor Details

    • Label

      public Label()
      Creates an empty Label.
    • Label

      public Label(String text)
      Creates a new instance with text content mode and the given text.
      Parameters:
      text - the text to set
    • Label

      public Label(String text, ContentMode contentMode)
      Creates a new instance with the given text and content mode.
      Parameters:
      text - the text to set
      contentMode - the content mode to use
    • Label

      public Label(Property contentSource)
      Creates a new instance of Label with text-contents read from given datasource.
      Parameters:
      contentSource -
    • Label

      public Label(Property contentSource, ContentMode contentMode)
      Creates a new instance of Label with text-contents read from given datasource.
      Parameters:
      contentSource -
      contentMode -
  • Method Details

    • 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.
    • setDescription

      public void setDescription(String description)
      Description copied from interface: FAbstractComponent
      Sets the component's description. See FAbstractComponent.getDescription() for more information on what the description is.
      Specified by:
      setDescription in interface FAbstractComponent
      Parameters:
      description - the new description string for the component.
      See Also:
    • setDescription

      public void setDescription(String description, ContentMode contentMode)
      Description copied from interface: FAbstractComponent
      Sets the component's description using given content mode. See FAbstractComponent.getDescription() for more information on what the description is.

      If the content mode is ContentMode.HTML the description is displayed as HTML in tooltips or directly in certain components so care should be taken to avoid creating the possibility for HTML injection and possibly XSS vulnerabilities.

      Specified by:
      setDescription in interface FAbstractComponent
      Parameters:
      description - the new description string for the component.
      contentMode - the content mode for the description
    • getCaption

      public String getCaption()
      Gets the caption of the component.
      Specified by:
      getCaption in interface FAbstractComponent
      Returns:
      the caption of the component or null if the caption is not set.
      See Also:
    • setCaption

      public void setCaption(String caption)
      Sets the caption of the component.
      Specified by:
      setCaption in interface FAbstractComponent
      Parameters:
      caption - the new caption for the component. If the caption is null, no caption is shown
    • setLabel

      public void setLabel(String label)
      Specified by:
      setLabel in interface com.vaadin.flow.component.HasLabel
    • getContentMode

      public ContentMode getContentMode()
      Gets the content mode of the label.
      Returns:
      the content mode of the label
      See Also:
    • setContentMode

      public void setContentMode(ContentMode contentMode)
      Sets the content mode of the label.
      Parameters:
      contentMode - the content mode to set
      See Also:
    • setValue

      public void setValue(String value)
      Sets the text to be shown in the label.
      Specified by:
      setValue in interface Property<String>
      Parameters:
      value - the text to show in the label, null is converted to an empty string
    • getValue

      public String getValue()
      Gets the text shown in the label.
      Specified by:
      getValue in interface Property<String>
      Returns:
      the text shown in the label, not null
    • setCaptionAsHtml

      public void setCaptionAsHtml(boolean captionAsHtml)
      Sets whether the caption is rendered as HTML.

      If set to true, the captions are rendered in the browser as HTML and the developer is responsible for ensuring no harmful HTML is used. If set to false, the caption is rendered in the browser as plain text.

      Note: Unlike the Label component in Vaadin 7/8, the script tag is removed when the caption is rendered as html The default is false, i.e. to render that caption as plain text.

      Specified by:
      setCaptionAsHtml in interface FAbstractComponent
      Parameters:
      captionAsHtml - true if the captions are rendered as HTML, false if rendered as plain text
    • isCaptionAsHtml

      public boolean isCaptionAsHtml()
      Checks whether captions are rendered as HTML

      The default is false, i.e. to render that caption as plain text.

      Specified by:
      isCaptionAsHtml in interface FAbstractComponent
      Returns:
      true if the captions are rendered as HTML, false if rendered as plain text
    • getChildren

      public Stream<com.vaadin.flow.component.Component> getChildren()
      Overrides:
      getChildren in class com.vaadin.flow.component.Component
    • setWidth

      public void setWidth(String width)
      Description copied from interface: Sizeable
      Sets the width of the component using String presentation.

      String presentation is similar to what is used in Cascading Style Sheets. Size can be length or percentage of available size.

      The empty string ("") or null will unset the width and set the units to pixels.

      See CSS specification for more details.

      Specified by:
      setWidth in interface FSizeable
      Specified by:
      setWidth in interface Sizeable
      Overrides:
      setWidth in class AbstractComponent
      Parameters:
      width - in CSS style string representation, null or empty string to reset
    • setWidth

      public void setWidth(float width, com.vaadin.flow.component.Unit unit)
      Description copied from interface: Sizeable
      Sets the width of the object. Negative number implies unspecified size (terminal is free to set the size).
      Specified by:
      setWidth in interface FSizeable
      Specified by:
      setWidth in interface Sizeable
      Overrides:
      setWidth in class AbstractComponent
      Parameters:
      width - the width of the object.
      unit - the unit used for the width.
    • getWidth

      public float getWidth()
      Description copied from interface: Sizeable
      Gets the width of the object. Negative number implies unspecified size (terminal is free to set the size).
      Specified by:
      getWidth in interface Sizeable
      Overrides:
      getWidth in class AbstractComponent
      Returns:
      width of the object in units specified by widthUnits property.
    • getWidthUnits

      public com.vaadin.flow.component.Unit getWidthUnits()
      Description copied from interface: Sizeable
      Gets the width property units.
      Specified by:
      getWidthUnits in interface FSizeable
      Specified by:
      getWidthUnits in interface Sizeable
      Overrides:
      getWidthUnits in class AbstractComponent
      Returns:
      units used in width property.
    • setHeight

      public void setHeight(String height)
      Description copied from interface: Sizeable
      Sets the height of the component using String presentation.

      String presentation is similar to what is used in Cascading Style Sheets. Size can be length or percentage of available size.

      The empty string ("") or null will unset the height and set the units to pixels.

      See CSS specification for more details.

      Specified by:
      setHeight in interface FSizeable
      Specified by:
      setHeight in interface Sizeable
      Overrides:
      setHeight in class AbstractComponent
      Parameters:
      height - in CSS style string representation
    • setHeight

      public void setHeight(float height, com.vaadin.flow.component.Unit unit)
      Description copied from interface: Sizeable
      Sets the height of the object. Negative number implies unspecified size (terminal is free to set the size).
      Specified by:
      setHeight in interface FSizeable
      Specified by:
      setHeight in interface Sizeable
      Overrides:
      setHeight in class AbstractComponent
      Parameters:
      height - the height of the object.
      unit - the unit used for the width.
    • getHeight

      public float getHeight()
      Description copied from interface: Sizeable
      Gets the height of the object. Negative number implies unspecified size (terminal is free to set the size).
      Specified by:
      getHeight in interface Sizeable
      Overrides:
      getHeight in class AbstractComponent
      Returns:
      height of the object in units specified by heightUnits property.
    • getHeightUnits

      public com.vaadin.flow.component.Unit getHeightUnits()
      Description copied from interface: Sizeable
      Gets the height property units.
      Specified by:
      getHeightUnits in interface FSizeable
      Specified by:
      getHeightUnits in interface Sizeable
      Overrides:
      getHeightUnits in class AbstractComponent
      Returns:
      units used in height property.
    • setSizeFull

      public void setSizeFull()
      Description copied from interface: Sizeable
      Sets the size to 100% x 100%.
      Specified by:
      setSizeFull in interface FSizeable
      Specified by:
      setSizeFull in interface Sizeable
      Overrides:
      setSizeFull in class AbstractComponent
    • setWidthFull

      public void setWidthFull()
      Description copied from interface: Sizeable
      Sets the width to 100%.
      Specified by:
      setWidthFull in interface FSizeable
      Specified by:
      setWidthFull in interface Sizeable
      Overrides:
      setWidthFull in class AbstractComponent
    • setHeightFull

      public void setHeightFull()
      Description copied from interface: Sizeable
      Sets the height to 100%.
      Specified by:
      setHeightFull in interface FSizeable
      Specified by:
      setHeightFull in interface Sizeable
      Overrides:
      setHeightFull in class AbstractComponent
    • setSizeUndefined

      public void setSizeUndefined()
      Description copied from interface: Sizeable
      Clears any size settings.
      Specified by:
      setSizeUndefined in interface FSizeable
      Specified by:
      setSizeUndefined in interface Sizeable
      Overrides:
      setSizeUndefined in class AbstractComponent
    • setWidthUndefined

      public void setWidthUndefined()
      Description copied from interface: Sizeable
      Clears any defined width.
      Specified by:
      setWidthUndefined in interface FSizeable
      Specified by:
      setWidthUndefined in interface Sizeable
      Overrides:
      setWidthUndefined in class AbstractComponent
    • setHeightUndefined

      public void setHeightUndefined()
      Description copied from interface: Sizeable
      Clears any defined height.
      Specified by:
      setHeightUndefined in interface FSizeable
      Specified by:
      setHeightUndefined in interface Sizeable
      Overrides:
      setHeightUndefined in class AbstractComponent
    • getStyleName

      public String getStyleName()
      Description copied from interface: FAbstractComponent
      Gets all user-defined CSS style names of a classic component. If the component has multiple style names defined, the return string is a space-separated list of style names. Built-in style names defined in Vaadin or GWT are not returned.

      The style names are returned only in the basic form in which they were added; each user-defined style name shows as two CSS style class names in the rendered HTML: one as it was given and one prefixed with the component-specific style name. Only the former is returned.

      Specified by:
      getStyleName in interface FAbstractComponent
      Returns:
      the style name or a space-separated list of user-defined style names of the component
      See Also:
    • setStyleName

      public void setStyleName(String style)
      Description copied from interface: FAbstractComponent
      Sets one or more user-defined style names of the classic component, replacing any previous user-defined styles. Multiple styles can be specified as a space-separated list of style names. The style names must be valid CSS class names and should not conflict with any built-in style names in Vaadin or GWT.
       Label label = new Label("This text has a lot of style");
       label.setStyleName("myonestyle myotherstyle");
       

      Each style name will occur in two versions: one as specified and one that is prefixed with the style name of the component. For example, if you have a Label component and give it "mystyle" style, the component will have both "mystyle" and "v-label-mystyle" styles. You could then style the component either with:

       .mystyle {background: blue;}
       

      or

       .v-label-mystyle {background: blue;}
       

      It is normally a good practice to use addStyleName() rather than this setter, as different software abstraction layers can then add their own styles without accidentally removing those defined in other layers.

      Specified by:
      setStyleName in interface FAbstractComponent
      Parameters:
      style - the new style or styles of the component as a space-separated list
      See Also:
    • setStyleName

      public void setStyleName(String style, boolean add)
      Description copied from interface: FAbstractComponent
      Adds or removes a style name. Multiple styles can be specified as a space-separated list of style names.

      If the add parameter is true, the style name is added to the component. If the add parameter is false, the style name is removed from the component.

      Functionally this is equivalent to using FAbstractComponent.addStyleName(String) or FAbstractComponent.removeStyleName(String)

      Specified by:
      setStyleName in interface FAbstractComponent
      Parameters:
      style - the style name to be added or removed
      add - true to add the given style, false to remove it
      See Also:
    • addStyleName

      public void addStyleName(String style)
      Description copied from interface: FAbstractComponent
      Adds one or more style names to the classic component. Multiple styles can be specified as a space-separated list of style names. The style name will be rendered as a HTML class name, which can be used in a CSS definition.
       Label label = new Label("This text has style");
       label.addStyleName("mystyle");
       

      Each style name will occur in two versions: one as specified and one that is prefixed with the style name of the component. For example, if you have a Label component and give it "mystyle" style, the component will have both "mystyle" and "v-label-mystyle" styles. You could then style the component either with:

       .mystyle {font-style: italic;}
       

      or

       .v-label-mystyle {font-style: italic;}
       
      Specified by:
      addStyleName in interface FAbstractComponent
      Parameters:
      style - the new style to be added to the component
      See Also:
    • addStyleNames

      public void addStyleNames(String... styles)
      Description copied from interface: FAbstractComponent
      Adds one or more style names to this classic component by using one or multiple parameters.
      Specified by:
      addStyleNames in interface FAbstractComponent
      Parameters:
      styles - the style name or style names to be added to the component
      See Also:
    • removeStyleName

      public void removeStyleName(String style)
      Description copied from interface: FAbstractComponent
      Removes one or more style names from the classic component. Multiple styles can be specified as a space-separated list of style names.

      The parameter must be a valid CSS style name. Only user-defined style names added with addStyleName() or setStyleName() can be removed; built-in style names defined in Vaadin or GWT can not be removed.

      Specified by:
      removeStyleName in interface FAbstractComponent
      Parameters:
      style - the style name or style names to be removed
      See Also:
    • removeStyleNames

      public void removeStyleNames(String... styles)
      Description copied from interface: FAbstractComponent
      Removes one or more style names from the classic component. Multiple styles can be specified by using multiple parameters.
      Specified by:
      removeStyleNames in interface FAbstractComponent
      Parameters:
      styles - the style name or style names to be removed
      See Also:
    • getPrimaryStyleName

      public String getPrimaryStyleName()
      Description copied from interface: FAbstractComponent
      Gets the primary style name of the classic component. See FAbstractComponent.setPrimaryStyleName(String) for a better description of the primary stylename.
      Specified by:
      getPrimaryStyleName in interface FAbstractComponent
    • setPrimaryStyleName

      public void setPrimaryStyleName(String style)
      Description copied from interface: FAbstractComponent
      Changes the primary style name of the classic component.

      The primary style name identifies the component when applying the CSS theme to the Component. By changing the style name all CSS rules targeted for that style name will no longer apply, and might result in the component not working as intended.

      Specified by:
      setPrimaryStyleName in interface FAbstractComponent
      Parameters:
      style - The new primary style name
    • getCustomStyles

      protected Collection<String> getCustomStyles()
      Description copied from class: AbstractComponent
      Returns the user added custom style names in an unmodifiable collection.
      Overrides:
      getCustomStyles in class AbstractComponent
      Returns:
      the user added custom style names
    • isEnabled

      public boolean isEnabled()
      Specified by:
      isEnabled in interface com.vaadin.flow.component.HasEnabled
    • setEnabled

      public void setEnabled(boolean enabled)
      Specified by:
      setEnabled in interface com.vaadin.flow.component.HasEnabled
    • setId

      public void setId(String id)
      Overrides:
      setId in class com.vaadin.flow.component.Component
    • getId

      public Optional<String> getId()
      Overrides:
      getId in class com.vaadin.flow.component.Component
    • getType

      public Class<String> getType()
      Description copied from interface: Property
      Returns the type of the Property. The methods getValue and setValue must be compatible with this type: one must be able to safely cast the value returned from getValue to the given type and pass any variable assignable to this type as an argument to setValue.
      Specified by:
      getType in interface Property<String>
      Returns:
      type of the Property
    • isPropertyReadOnly

      public boolean isPropertyReadOnly()
      Description copied from interface: Property
      Tests if the Property is in read-only mode. In read-only mode calls to the method setValue will throw ReadOnlyException and will not modify the value of the Property.
      Specified by:
      isPropertyReadOnly in interface Property<String>
      Returns:
      true if the Property is in read-only mode, false if it's not
    • setPropertyReadOnly

      public void setPropertyReadOnly(boolean newStatus)
      Description copied from interface: Property
      Sets the Property's read-only mode to the specified status. This functionality is optional, but all properties must implement the isReadOnly mode query correctly.
      Specified by:
      setPropertyReadOnly in interface Property<String>
      Parameters:
      newStatus - new read-only status of the Property
    • getPropertyDataSource

      public Property getPropertyDataSource()
      Gets the viewing data-source property.
      Specified by:
      getPropertyDataSource in interface Property.Viewer
      Returns:
      the data source property.
      See Also:
    • setPropertyDataSource

      public void setPropertyDataSource(Property newDataSource)
      Sets the property as data-source for viewing. Since Vaadin 7.2 a ValueChangeEvent is fired if the new value is different from previous.
      Specified by:
      setPropertyDataSource in interface Property.Viewer
      Parameters:
      newDataSource - the new data source Property
      See Also:
    • valueChange

      public void valueChange(Property.ValueChangeEvent event)
      Listens the value change events from data source.
      Specified by:
      valueChange in interface Property.ValueChangeListener
      Parameters:
      event - value change event object
      See Also:
    • fireValueChange

      protected void fireValueChange()
      Emits the options change event.
    • getConverter

      public com.vaadin.flow.data.converter.Converter<String,Object> getConverter()
      Gets the converter used to convert the property data source value to the label value.
      Returns:
      The converter or null if none is set.
    • setConverter

      public void setConverter(com.vaadin.flow.data.converter.Converter<String,?> converter)
      Sets the converter used to convert the label value to the property data source type. The converter must have a presentation type of String.
      Parameters:
      converter - The new converter to use.
    • addValueChangeListener

      public void addValueChangeListener(Property.ValueChangeListener listener)
      Adds the value change listener.
      Specified by:
      addValueChangeListener in interface Property.ValueChangeNotifier
      Parameters:
      listener - the Listener to be added.
      See Also:
    • removeValueChangeListener

      public void removeValueChangeListener(Property.ValueChangeListener listener)
      Removes the value change listener.
      Specified by:
      removeValueChangeListener in interface Property.ValueChangeNotifier
      Parameters:
      listener - the Listener to be removed.
      See Also: