Posted by Darryl Burke on March 22, 2009
Many Swing components accommodate an icon. What if you would like to display more than one icon on the same component, or show a custom icon on a component like JCheckBox or JRadioButton, in addition to the icon that the Look and Feel displays by default?
DualIcon to the rescue! This simple class facilitates making one icon from two. The composite icon can be applied to the component using setIcon.
DualIcon exposes two public constructors, one of which allows flexibility in the relative placement of the icons and the gap between them. The parameters for horizontal and vertical position are inherited from the familiar SwingConstants interface, which is implemented by the class.
Possible uses for DualIcon include displaying two icons in a JOptionPane, possibly a company logo alongside the usual icon, or adding a custom icon to a JCheckBox or JRadioButton. A set of classes that inherit from DualIcon to provide specialized functionality for the latter purpose is the subject of a later blog posting.