Java Reference
In-Depth Information
The items for the other menus are created in the same way using the addMenuItem() method. We create
the Action objects in the expressions for the arguments to the method, as they are relatively simple
expressions. Because we store the references to the Action objects, they will be available later when we
want to create toolbar buttons corresponding to the menu items. Note that we have omitted the accelerators
for the E lements menu items here on the grounds that they were not exactly standard or convenient.
You may be wondering at this point why we have to set the accelerator key for a menu item explicitly,
and why an accelerator key stored within an Action object is not added to the menu item
automatically. There's a very good reason for not having the add() method automatically set an
accelerator key from an Action object. Pressing an accelerator key combination would be the
equivalent of clicking any item created from a corresponding Action object. This could be a toolbar
button and a menu item. Thus if the accelerator keys were automatically set for both components, you
would get events from both components when you press the accelerator key combination - not exactly
what you would want as each action would then be carried out twice!
If you try out the color menus you should see the background color change. If it doesn't there's
something wrong somewhere.
Now we have the menus set up using action objects, we are ready to tackle adding a toolbar to
our application.
Adding a Toolbar
A toolbar is a bar, usually positioned below the menu bar, which contains a row of buttons that typically
provides a more direct route to menu options. We could add a toolbar to the Sketcher program for the
menu items that are likely to be most popular. Just so that you know where we are heading, the kind of
toolbar we will end up with ultimately is shown below.
The four buttons in the first group are for the most used functions in the file menu. The other two
groups of four buttons select the element type and element color respectively. So how are we going to
put this toolbar together?
Search WWH ::




Custom Search