Posted by Rob Camick on April 2, 2009
Whenever people think of a Icon, they usually think of an image of some kind. The image is a simple picture that is used to represent something (whatever that may be). The general purpose Icon class provided by the JDK is the ImageIcon class. Maybe it is time for another type of Icon? How about a TextIcon?
The TextIcon class creates an Icon that represents a string of text. This Icon can then be added to any Swing component that supports icons. TextIcon will support two different layout styles:
- Horizontal (default) - the text is rendered just like regular text
- Vertical – the text is broken up into single characters and each character is rendered on a separate line from top to bottom
In order to calculate the size of the icon correctly, the TextIcon must know the Swing component the icon will be added to. This way the proper FontMetrics can be used in the size calculation. Also, by default, the TextIcon will render the text using the Font and foreground Color of this component. However, there are a few methods that will allow you to change the default rendering of the text:
- setPadding – add a few extra pixels of space at the start and end of the rendered text
- setFont – override the Font used to render the text
- setForeground – override the foreground Color used to render the text
The following image shows of couple of examples of components using the TextIcon:
Yes, the horizontal button is rather boring, after all it looks just like a regular button with text. Well, stay tuned for my next blog entry which may give you some more ideas. To me the vertical button looks a little wide, but that can easily be controlled by playing with the button margins. The tabbed pane example may show the most practical usage for the TextIcon. Also, note how the second tab contains an ImageIcon as well as the TextIcon. This is a great example of using the CompoundIcon. The code to create the second tab and icon was something like:
TextIcon two = new TextIcon(tabbedPane, "Two", TextIcon.Layout.VERTICAL);
ImageIcon image = new ImageIcon(...);
CompoundIcon ci = new CompoundIcon(CompoundIcon.Axis.Y_AXIS, image, two);
tabbedPane.addTab("", ci, somePanel);
Don’t forget to try playing with a vertical JLabel, JCheckBox or JRadioButton. I’m sure you can come up with other creative uses as well.
Update: This class was updated on November 14, 2011 to get the anti aliasing properties from the Toolkit class. I’ve tested this on Windows XP using the Metal and Windows LAF. I’ve been warned that this code may still not paint the text exactly as you would see the text normally rendered on a component for other platforms or LAF’s. Let me know if you experience any problems.
Note: I have come across an issue when painting text strings that start with a capital “W”. The width of the string is not being calculated correctly using the FontMetrics class and the leading pixel of the “W” is being truncated. This only appears to happen on XP using font size 11, or 12. The workaround to this problem is to use setPadding(1) on the TextIcon.