Class Label

  • All Implemented Interfaces:
    ClientConnector, Sizeable, com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, java.io.Serializable

    @Tag("div")
    @CssImport("./label.css")
    @JsModule("@vaadin/vaadin-lumo-styles/font-icons.js")
    public class Label
    extends AbstractComponent
    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:
    Serialized Form
    • Constructor Detail

      • Label

        public Label()
        Creates an empty Label.
      • Label

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

        public Label​(java.lang.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
    • Method Detail

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

        public java.lang.String getCaption()
        Gets the caption of the component.
        Overrides:
        getCaption in class AbstractComponent
        Returns:
        the caption of the component or null if the caption is not set.
        See Also:
        setCaption(String)
      • setCaption

        public void setCaption​(java.lang.String caption)
        Sets the caption of the component.
        Overrides:
        setCaption in class AbstractComponent
        Parameters:
        caption - the new caption for the component. If the caption is null, no caption is shown
      • getContentMode

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

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

        public void setValue​(java.lang.String value)
        Sets the text to be shown in the label.
        Parameters:
        value - the text to show in the label, null is converted to an empty string
      • getValue

        public java.lang.String getValue()
        Gets the text shown in the label.
        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.

        Overrides:
        setCaptionAsHtml in class AbstractComponent
        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.

        Overrides:
        isCaptionAsHtml in class AbstractComponent
        Returns:
        true if the captions are rendered as HTML, false if rendered as plain text
      • getChildren

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

        public void setWidth​(java.lang.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 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,
                             Sizeable.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 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.
      • setHeight

        public void setHeight​(java.lang.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 Sizeable
        Overrides:
        setHeight in class AbstractComponent
        Parameters:
        height - in CSS style string representation
      • setHeight

        public void setHeight​(float height,
                              Sizeable.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 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.
      • getStyleName

        public java.lang.String getStyleName()
        Description copied from class: AbstractComponent
        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.

        Overrides:
        getStyleName in class AbstractComponent
        Returns:
        the style name or a space-separated list of user-defined style names of the component
        See Also:
        AbstractComponent.setStyleName(String), AbstractComponent.addStyleName(String), AbstractComponent.removeStyleName(String)
      • setStyleName

        public void setStyleName​(java.lang.String style)
        Description copied from class: AbstractComponent
        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.

        Overrides:
        setStyleName in class AbstractComponent
        Parameters:
        style - the new style or styles of the component as a space-separated list
        See Also:
        AbstractComponent.getStyleName(), AbstractComponent.addStyleName(String), AbstractComponent.removeStyleName(String)
      • addStyleName

        public void addStyleName​(java.lang.String style)
        Description copied from class: AbstractComponent
        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;}
         
        Overrides:
        addStyleName in class AbstractComponent
        Parameters:
        style - the new style to be added to the component
        See Also:
        AbstractComponent.getStyleName(), AbstractComponent.setStyleName(String), AbstractComponent.removeStyleName(String)
      • setPrimaryStyleName

        public void setPrimaryStyleName​(java.lang.String style)
        Description copied from class: AbstractComponent
        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.

        Overrides:
        setPrimaryStyleName in class AbstractComponent
        Parameters:
        style - The new primary style name
      • getCustomStyles

        protected java.util.Collection<java.lang.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()
        Description copied from class: AbstractComponent
        Tests whether the component is enabled or not. A user can not interact with disabled components. Disabled components are rendered in a style that indicates the status, usually in gray color. Children of a disabled component are also disabled. Components are enabled by default.

        As a security feature, all updates for disabled components are blocked on the server-side.

        Note that this method only returns the status of the component and does not take parents into account. Even though this method returns true the component can be disabled to the user if a parent is disabled.

        Specified by:
        isEnabled in interface com.vaadin.flow.component.HasEnabled
        Overrides:
        isEnabled in class AbstractComponent
        Returns:
        true if the component and its parent are enabled, false otherwise.
      • setEnabled

        public void setEnabled​(boolean enabled)
        Description copied from class: AbstractComponent
        Enables or disables the component. The user can not interact with disabled components, which are shown with a style that indicates the status, usually shaded in light gray color. Components are enabled by default.
         Button enabled = new Button("Enabled");
         enabled.setEnabled(true); // The default
         layout.addComponent(enabled);
        
         Button disabled = new Button("Disabled");
         disabled.setEnabled(false);
         layout.addComponent(disabled);
         
        Specified by:
        setEnabled in interface com.vaadin.flow.component.HasEnabled
        Overrides:
        setEnabled in class AbstractComponent
        Parameters:
        enabled - a boolean value specifying if the component should be enabled or not
      • setId

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

        public java.util.Optional<java.lang.String> getId()
        Overrides:
        getId in class com.vaadin.flow.component.Component