Class Label

All Implemented Interfaces:
IFigure, PositionConstants

public class Label extends Figure implements PositionConstants
A figure that can display text and/or an image.
  • Constructor Details

    • Label

      public Label()
      Construct an empty Label.
      Since:
      2.0
    • Label

      public Label(String s)
      Construct a Label with passed String as its text.
      Parameters:
      s - the label text
      Since:
      2.0
    • Label

      public Label(Image i)
      Construct a Label with passed Image as its icon.
      Parameters:
      i - the label image
      Since:
      2.0
    • Label

      public Label(String s, Image i)
      Construct a Label with passed String as text and passed Image as its icon.
      Parameters:
      s - the label text
      i - the label image
      Since:
      2.0
  • Method Details

    • calculateLabelSize

      protected Dimension calculateLabelSize(Dimension txtSize)
      Calculates the size of the Label using the passed Dimension as the size of the Label's text.
      Parameters:
      txtSize - the precalculated size of the label's text
      Returns:
      the label's size
      Since:
      2.0
    • calculateSubStringTextSize

      protected Dimension calculateSubStringTextSize()
      Calculates the size of the Label's text size. The text size calculated takes into consideration if the Label's text is currently truncated. If text size without considering current truncation is desired, use calculateTextSize().
      Returns:
      the size of the label's text, taking into account truncation
      Since:
      2.0
    • calculateTextSize

      protected Dimension calculateTextSize()
      Calculates and returns the size of the Label's text. Note that this Dimension is calculated using the Label's full text, regardless of whether or not its text is currently truncated. If text size considering current truncation is desired, use calculateSubStringTextSize().
      Returns:
      the size of the label's text, ignoring truncation
      Since:
      2.0
    • getIcon

      public Image getIcon()
      Returns the Label's icon.
      Returns:
      the label icon
      Since:
      2.0
    • getIconAlignment

      public int getIconAlignment()
      Returns the current alignment of the Label's icon. The default is PositionConstants.CENTER.
      Returns:
      the icon alignment
      Since:
      2.0
    • getIconBounds

      public Rectangle getIconBounds()
      Returns the bounds of the Label's icon.
      Returns:
      the icon's bounds
      Since:
      2.0
    • getIconLocation

      protected Point getIconLocation()
      Returns the location of the Label's icon relative to the Label.
      Returns:
      the icon's location
      Since:
      2.0
    • getIconTextGap

      public int getIconTextGap()
      Returns the gap in pixels between the Label's icon and its text.
      Returns:
      the gap
      Since:
      2.0
    • getMinimumSize

      public Dimension getMinimumSize(int w, int h)
      Description copied from interface: IFigure
      Returns a hint indicating the smallest desireable size for the IFigure. The returned dimension may be by reference, and it must not be modified by the caller.
      Specified by:
      getMinimumSize in interface IFigure
      Overrides:
      getMinimumSize in class Figure
      Parameters:
      w - the width hint
      h - the height hint
      Returns:
      The minimum size
      See Also:
    • getPreferredSize

      public Dimension getPreferredSize(int wHint, int hHint)
      Description copied from interface: IFigure
      Returns the preferred size for this IFigure using the provided width and height hints. The returned dimension may be by reference, and it must not be modified by the caller. A value of -1 indicates that there is no constraint in that direction.
      Specified by:
      getPreferredSize in interface IFigure
      Overrides:
      getPreferredSize in class Figure
      Parameters:
      wHint - a width hint
      hHint - a height hint
      Returns:
      The preferred size
      See Also:
    • getSubStringText

      public String getSubStringText()
      Calculates the amount of the Label's current text will fit in the Label, including an elipsis "..." if truncation is required.
      Returns:
      the substring
      Since:
      2.0
    • getSubStringTextSize

      protected Dimension getSubStringTextSize()
      Returns the size of the Label's current text. If the text is currently truncated, the truncated text with its ellipsis is used to calculate the size.
      Returns:
      the size of this label's text, taking into account truncation
      Since:
      2.0
    • getText

      public String getText()
      Returns the text of the label. Note that this is the complete text of the label, regardless of whether it is currently being truncated. Call getSubStringText() to return the label's current text contents with truncation considered.
      Returns:
      the complete text of this label
      Since:
      2.0
    • getTextAlignment

      public int getTextAlignment()
      Returns the current alignment of the Label's text. The default text alignment is PositionConstants.CENTER.
      Returns:
      the text alignment
    • getTextBounds

      public Rectangle getTextBounds()
      Returns the bounds of the label's text. Note that the bounds are calculated using the label's complete text regardless of whether the label's text is currently truncated.
      Returns:
      the bounds of this label's complete text
      Since:
      2.0
    • getTextLocation

      protected Point getTextLocation()
      Returns the location of the label's text relative to the label.
      Returns:
      the text location
      Since:
      2.0
    • getTextPlacement

      public int getTextPlacement()
      Returns the current placement of the label's text relative to its icon. The default text placement is PositionConstants.EAST.
      Returns:
      the text placement
      Since:
      2.0
    • getTextSize

      protected Dimension getTextSize()
      Returns the size of the label's complete text. Note that the text used to make this calculation is the label's full text, regardless of whether the label's text is currently being truncated and is displaying an ellipsis. If the size considering current truncation is desired, call getSubStringTextSize().
      Returns:
      the size of this label's complete text
      Since:
      2.0
    • invalidate

      public void invalidate()
      Description copied from interface: IFigure
      Invalidates this IFigure. If this figure has a LayoutManager, then LayoutManager.invalidate() should be called on that layout.
      Specified by:
      invalidate in interface IFigure
      Overrides:
      invalidate in class Figure
      See Also:
    • isTextTruncated

      public boolean isTextTruncated()
      Returns true if the label's text is currently truncated and is displaying an ellipsis, false otherwise.
      Returns:
      true if the label's text is truncated
      Since:
      2.0
    • paintFigure

      protected void paintFigure(Graphics graphics)
      Description copied from class: Figure
      Paints this Figure's primary representation, or background. Changes made to the graphics to the graphics current state will not affect the subsequent calls to Figure.paintClientArea(Graphics) and Figure.paintBorder(Graphics). Furthermore, it is safe to call graphics.restoreState() within this method, and doing so will restore the graphics to its original state upon entry.
      Overrides:
      paintFigure in class Figure
      Parameters:
      graphics - The Graphics used to paint
      See Also:
    • setIcon

      public void setIcon(Image image)
      Sets the label's icon to the passed image.
      Parameters:
      image - the new label image
      Since:
      2.0
    • setIconAlignment

      public void setIconAlignment(int align)
      This method sets the alignment of the icon within the bounds of the label. If the label is larger than the icon, then the icon will be aligned according to this alignment. Valid values are:
      Parameters:
      align - the icon alignment
      Since:
      2.0
    • setIconDimension

      @Deprecated public void setIconDimension(Dimension d)
      Deprecated.
      the icon is automatically displayed at 1:1
      Sets the label's icon size to the passed Dimension.
      Parameters:
      d - the new icon size
      Since:
      2.0
    • setIconTextGap

      public void setIconTextGap(int gap)
      Sets the gap in pixels between the label's icon and text to the passed value. The default is 4.
      Parameters:
      gap - the gap
      Since:
      2.0
    • setLabelAlignment

      public void setLabelAlignment(int align)
      Sets the alignment of the label (icon and text) within the figure. If this figure's bounds are larger than the size needed to display the label, the label will be aligned accordingly. Valid values are:
      Parameters:
      align - label alignment
    • setText

      public void setText(String s)
      Sets the label's text.
      Parameters:
      s - the new label text
      Since:
      2.0
    • setTextAlignment

      public void setTextAlignment(int align)
      Sets the alignment of the text relative to the icon within the label. The text alignment must be orthogonal to the text placement. For example, if the placement is EAST, then the text can be aligned using TOP, CENTER, or BOTTOM. Valid values are:
      Parameters:
      align - the text alignment
      Since:
      2.0
      See Also:
    • setTextPlacement

      public void setTextPlacement(int where)
      Sets the placement of the text relative to the icon within the label. Valid values are:
      Parameters:
      where - the text placement
      Since:
      2.0
    • getTextUtilities

      public TextUtilities getTextUtilities()
      Gets the TextUtilities instance to be used in measurement calculations.
      Returns:
      a TextUtilities instance
      Since:
      3.4
    • getTruncationString

      protected String getTruncationString()
      Gets the string that will be appended to the text when the label is truncated. By default, this returns an ellipsis.
      Returns:
      the string to append to the text when truncated
      Since:
      3.4
    • getIconSize

      protected Dimension getIconSize()
      Gets the icon size
      Returns:
      the icon size
      Since:
      3.4
    • getLabelAlignment

      public int getLabelAlignment()
      Returns the alignment of the entire label (icon and text). The default label alignment is PositionConstants.CENTER.
      Returns:
      the label alignment
      Since:
      3.5