Java Reference
In-Depth Information
private void createRadioButtonDropDown(
JMenu menu, Action[] actions, Action
selected) {
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem item = null;
for(Action action : actions) {
group.add(menu.add(item = new JRadioButtonMenuItem(action)));
if(action == selected) {
item.setSelected(true);
// This is
default selected
}
}
}
Directory "Sketcher 6 using Action objects"
The first parameter is the menu to which the items are to be added. This makes use of an array of actions
that is passed to it as the second argument, and the third argument is an Action reference that identifies
the menu item that is to be selected initially. The loop iterates over the actions in the array. Each itera-
tion creates a menu item from the action and adds it to the menu. The add() method for JMenu returns a
reference to the item that was added so this reference is passed to the add() method for group to add it
to the button group. Each action is compared with the one supplied as the third argument to identify the
menu item to be set as selected.
You use the createRadioButtonDropDown() in the creation of the Color menu, too:
private void createColorMenu() {
createElementColorActions();
colorMenu = new JMenu("Color");
// Create
Elements menu
colorMenu.setMnemonic('C');
// Create
shortcut
createRadioButtonDropDown(colorMenu, colorActions, blueAction);
menuBar.add(colorMenu);
// Add the
color menu
}
Directory "Sketcher 6 using Action objects"
This is almost identical to the method that creates the Elements menu.
If you recompile and run Sketcher after you have made the changes I have described, you get a window
that looks like the one shown in Figure 18-7 .
 
Search WWH ::




Custom Search