Java Reference
In-Depth Information
This calls the setState() method for each menu item. If the argument to the method is true , the check
mark is set, and if it is false , it isn't. Clearly this sets the check mark only for the item that corresponds to
the color referenced by color . This is quite straightforward, but there is a better way.
A ButtonGroup object works with JCheckBoxMenuItem objects because they have AbstractButton as
a base class. Therefore, you could add these menu items to their own button group in the SketcherFrame
constructor, and it is all taken care of for you. The ButtonGroup object tracks the state of all of the buttons
in the group. When any button is turned on, all the others are turned off, so only one button in the group
can be on at one time. Add the following code — it could go anywhere after the items have been created
but place it following the code that adds the items to the Color menu for consistency with the element type
code:
ButtonGroup colors = new ButtonGroup();
// Color menu items button group
colors.add(redItem);
colors.add(yellowItem);
colors.add(greenItem);
colors.add(blueItem);
Directory "Sketcher 5 with element color listeners"
Now the Color menu check marks are set automatically, so you can forget about them.
USING ACTIONS
One difficulty with the code that you have added to support the menus is that it is very menu specific. What
I mean by this is that if you are going to do a proper job on the Sketcher application, you undoubtedly want
it to have a toolbar. The toolbar will surely have a whole bunch of buttons that perform exactly the same
actions as the menu items you have just implemented, so you'll be in the business of doing the same thing all
over again in the toolbar context. Of course, the only reason I brought it up, as I'm sure you've anticipated,
is that there is another way of working with menus, and that is to use an Action object.
An Action object is a bit of a strange beast. It can be quite hard to understand at first, so I'm taking it
slowly. First of all let's look at what is meant by an “action" here, as it is a precise term in this context. An
action is an object of any class that implements the javax.swing.Action interface. This interface declares
methods that operate on an A ction object — for example, storing properties relating to the action, enabling
it, and disabling it. The Action interface happens to extend the ActionListener interface, so an A ction
object is a listener as well as an action. Now that you know an Action object can get and set properties and
is also a listener, how does that help us in implementing the Sketcher GUI?
One answer is in the last capability of an Action object. Some Swing components, including those of
type JMenu and JToolBar , have an add() method that accepts an argument of type Action . When you
add an Action object to these components using the add() method, the method creates a component from
the Action object that is automatically of the right type. If you add an Action object to a JMenu object, a
JMenuItem is created and returned by the add() method. On the other hand, when you add exactly the same
Action object to a JToolBar object, an object of type JButton is created and returned. This means that you
Search WWH ::




Custom Search