Java Reference
In-Depth Information
the color the menu item sets up. The
actionPerformed()
method just stores the
Color
object from the
listener object in the
elementColor
member of the
SketcherFrame
object.
Of course, the menu doesn't quite work as it should. The
Color
menu item check marks are not being set
correctly, as you can see in
Figure 18-6
. You want an exclusive check, as with the radio buttons; having
more than one color checked at one time doesn't make sense.
Fixing the Color Menu Check Marks
One way to deal with the problem is to make the listener object for a color menu item set the check marks
for all the menu items. You could code this in the
ColorListener
inner class to
SketcherFrame
:
class ColorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
elementColor = color;
// Set the checks for all menu items
redItem.setState(color.equals(RED));
greenItem.setState(color.equals(GREEN));
blueItem.setState(color.equals(BLUE));
yellowItem.setState(color.equals(YELLOW));
}
// Rest of the class as before...
}