Class TextIcon

  • All Implemented Interfaces:
    PropertyChangeListener, EventListener, Icon

    public class TextIcon
    extends Object
    implements Icon, PropertyChangeListener
    The TextIcon will paint a String of text as an Icon. The Icon can be used by any Swing component that supports icons. TextIcon supports two different layout styles:
    • Horizontally - does normal rendering of the text by using the Graphics.drawString(...) method
    • Vertically - Each character is displayed on a separate line
    TextIcon was designed to be rendered on a specific JComponent as it requires FontMetrics information in order to calculate its size and to do the rendering. Therefore, it should only be added to component it was created for. By default the text will be rendered using the Font and foreground color of its associated component. However, this class does allow you to override these properties. Also starting in JDK6 the desktop renderering hints will be used to renderer the text. For versions not supporting the rendering hints antialiasing will be turned on.
    • Constructor Detail

      • TextIcon

        public TextIcon​(JComponent component,
                        String text)
        Convenience constructor to create a TextIcon with a HORIZONTAL layout.
        Parameters:
        component - the component to which the icon will be added
        text - the text to be rendered on the Icon
      • TextIcon

        public TextIcon​(JComponent component,
                        String text,
                        TextIcon.Layout layout)
        Create a TextIcon specifying all the properties.
        Parameters:
        component - the component to which the icon will be added
        text - the text to be rendered on the Icon
        layout - specify the layout of the text. Must be one of the Layout enums: HORIZONTAL or VERTICAL
    • Method Detail

      • getLayout

        public TextIcon.Layout getLayout()
        Get the Layout enum
        Returns:
        the Layout enum
      • getText

        public String getText()
        Get the text String that will be rendered on the Icon
        Returns:
        the text of the Icon
      • setText

        public void setText​(String text)
        Set the text to be rendered on the Icon
        Parameters:
        text - the text to be rendered on the Icon
      • getFont

        public Font getFont()
        Get the Font used to render the text. This will default to the Font of the component unless the Font has been overridden by using the setFont() method.
        Returns:
        the Font used to render the text
      • setFont

        public void setFont​(Font font)
        Set the Font to be used for rendering the text
        Parameters:
        font - the Font to be used for rendering the text
      • getForeground

        public Color getForeground()
        Get the foreground Color used to render the text. This will default to the foreground Color of the component unless the foreground Color has been overridden by using the setForeground() method.
        Returns:
        the Color used to render the text
      • setForeground

        public void setForeground​(Color foreground)
        Set the foreground Color to be used for rendering the text
        Parameters:
        foreground - the foreground Color to be used for rendering the text
      • getPadding

        public int getPadding()
        Get the padding used when rendering the text
        Returns:
        the padding specified in pixels
      • setPadding

        public void setPadding​(int padding)
        By default the size of the Icon is based on the size of the rendered text. You can specify some padding to be added to the start and end of the text when it is rendered.
        Parameters:
        padding - the padding amount in pixels
      • getIconWidth

        public int getIconWidth()
        Gets the width of this icon.
        Specified by:
        getIconWidth in interface Icon
        Returns:
        the width of the icon in pixels.
      • getIconHeight

        public int getIconHeight()
        Gets the height of this icon.
        Specified by:
        getIconHeight in interface Icon
        Returns:
        the height of the icon in pixels.
      • paintIcon

        public void paintIcon​(Component c,
                              Graphics g,
                              int x,
                              int y)
        Paint the icons of this compound icon at the specified location
        Specified by:
        paintIcon in interface Icon
        Parameters:
        c - The component to which the icon is added
        g - the graphics context
        x - the X coordinate of the icon's top-left corner
        y - the Y coordinate of the icon's top-left corner