public class TextIcon
extends java.lang.Object
implements javax.swing.Icon, java.beans.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:
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.
| Modifier and Type | Class and Description |
|---|---|
static class |
TextIcon.Layout |
| Constructor and Description |
|---|
TextIcon(javax.swing.JComponent component,
java.lang.String text)
Convenience constructor to create a TextIcon with a HORIZONTAL layout.
|
TextIcon(javax.swing.JComponent component,
java.lang.String text,
TextIcon.Layout layout)
Create a TextIcon specifying all the properties.
|
| Modifier and Type | Method and Description |
|---|---|
java.awt.Font |
getFont()
Get the Font used to render the text.
|
java.awt.Color |
getForeground()
Get the foreground Color used to render the text.
|
int |
getIconHeight()
Gets the height of this icon.
|
int |
getIconWidth()
Gets the width of this icon.
|
TextIcon.Layout |
getLayout()
Get the Layout enum
|
int |
getPadding()
Get the padding used when rendering the text
|
java.lang.String |
getText()
Get the text String that will be rendered on the Icon
|
void |
paintIcon(java.awt.Component c,
java.awt.Graphics g,
int x,
int y)
Paint the icons of this compound icon at the specified location
|
void |
propertyChange(java.beans.PropertyChangeEvent e) |
void |
setFont(java.awt.Font font)
Set the Font to be used for rendering the text
|
void |
setForeground(java.awt.Color foreground)
Set the foreground Color to be used for rendering the text
|
void |
setPadding(int padding)
By default, the size of the Icon is based on the size of the rendered text.
|
void |
setText(java.lang.String text)
Set the text to be rendered on the Icon
|
public TextIcon(javax.swing.JComponent component,
java.lang.String text)
component - The component to which the icon will be addedtext - The text to be rendered on the Iconpublic TextIcon(javax.swing.JComponent component,
java.lang.String text,
TextIcon.Layout layout)
component - The component to which the icon will be addedtext - The text to be rendered on the Iconlayout - Specify the layout of the text. Must be one of the Layout enums: HORIZONTAL or
VERTICALpublic TextIcon.Layout getLayout()
public java.lang.String getText()
public void setText(java.lang.String text)
text - The text to be rendered on the Iconpublic java.awt.Font getFont()
public void setFont(java.awt.Font font)
font - The Font to be used for rendering the textpublic java.awt.Color getForeground()
public void setForeground(java.awt.Color foreground)
foreground - The foreground Color to be used for rendering the textpublic int getPadding()
public void setPadding(int padding)
padding - The padding amount in pixelspublic int getIconWidth()
getIconWidth in interface javax.swing.Iconpublic int getIconHeight()
getIconHeight in interface javax.swing.Iconpublic void paintIcon(java.awt.Component c,
java.awt.Graphics g,
int x,
int y)
paintIcon in interface javax.swing.Iconc - The component to which the icon is addedg - the graphics contextx - the X coordinate of the icon's top-left cornery - the Y coordinate of the icon's top-left cornerpublic void propertyChange(java.beans.PropertyChangeEvent e)
propertyChange in interface java.beans.PropertyChangeListener