Java Reference
In-Depth Information
Table 6-12.
JCheckBoxMenuItem UIResource Elements (Continued)
Property String
Object Type
CheckBoxMenuItem.checkIcon
Icon
CheckBoxMenuItem.commandSound
String
CheckBoxMenuItem.disabledForeground
Color
CheckBoxMenuItem.font
Font
CheckBoxMenuItem.foreground
Color
CheckBoxMenuItem.gradient
List
CheckBoxMenuItem.margin
Insets
CheckBoxMenuItem.opaque
Boolean
CheckBoxMenuItem.selectionBackground
Color
CheckBoxMenuItem.selectionForeground
Color
CheckBoxMenuItemUI
String
The
Icon
associated with the
CheckBoxMenuItem.checkIcon
property key is the one displayed on
the
JCheckBoxMenuItem
. If you don't like the default icon, you can change it with the following line
of source, assuming the new icon has already been defined and created:
UIManager.put("CheckBoxMenuItem.checkIcon", someIcon);
For this new icon to display an appropriate selected image, the
Icon
implementation
must check the state of the associated menu component within its
paintIcon()
method. The
DiamondIcon
created in Chapter 4 wouldn't work for this icon because it doesn't ask the compo-
nent for its state. Instead, the state is fixed at constructor time. Listing 6-4 shows a class that
represents one icon that could be used.
Listing 6-4.
State-Aware Icon Definition
import java.awt.*;
import javax.swing.*;
public class DiamondAbstractButtonStateIcon implements Icon {
private final int width = 10;
private final int height = 10;
private Color color;
private Polygon polygon;
public DiamondAbstractButtonStateIcon(Color color) {
this.color = color;
initPolygon();
}