Class Label

  • Direct Known Subclasses:
    Button

    public class Label
    extends Widget
    A label is a widget that displays a text.

    This example shows a simple label:

     Label label = new Label("Hello World");
     
    Simple label.

    This example shows a styled label:

     Label label = new Label("Hello World");
    
     CascadingStylesheet stylesheet = new CascadingStylesheet();
     desktop.setStylesheet(stylesheet);
    
     EditableStyle labelStyle = stylesheet.getSelectorStyle(new TypeSelector(Label.class));
     labelStyle.setColor(Colors.NAVY);
     labelStyle.setBackground(new RectangularBackground(Colors.CYAN));
     labelStyle.setFont(Font.getFont("/fonts/source_sans_pro_24.ejf"));
     
    Styled label.
    • Constructor Detail

      • Label

        public Label()
        Creates a label with an empty text.
      • Label

        public Label​(String text)
        Creates a label with the given text to display.
        Parameters:
        text - the text to display.
      • Label

        protected Label​(String text,
                        boolean enabled)
        Creates a label with the given text to display and its enabled state.
        Parameters:
        text - the text to display.
        enabled - true if this label is to be enabled, false otherwise.
    • Method Detail

      • getText

        public String getText()
        Gets the text displayed on this label.
        Returns:
        the text displayed on this label.
      • setText

        public void setText​(String text)
        Sets the text to display on this label.
        Parameters:
        text - the text to display on this label.
      • renderContent

        protected void renderContent​(GraphicsContext g,
                                     int contentWidth,
                                     int contentHeight)
        Description copied from class: Widget
        Renders the content of the widget without the border, margin and padding specified in the style.

        The given graphics context is translated and clipped according to the given bounds (the border, margin and padding are applied on this graphics context before).

        Specified by:
        renderContent in class Widget
        Parameters:
        g - the graphics context where to render the content of the widget.
        contentWidth - the width of the content area.
        contentHeight - the height of the content area.
      • computeContentOptimalSize

        protected void computeContentOptimalSize​(Size size)
        Description copied from class: Widget
        Computes the optimal size of the widget.

        This method does not consider the border, margin, padding and dimension specified in the style.

        The given size is the available size for this widget in its parent. A width or a height equal to Widget#NO_CONSTRAINT means that there is no constraint on this dimension.

        The given size is modified to set the optimal size.

        Specified by:
        computeContentOptimalSize in class Widget
        Parameters:
        size - the size available for the content.