Java Reference
In-Depth Information
for this allows the same code to set checks for both the Elements menu and the Color menu. You can imple-
ment this as a SketcherFrame method:
private void setChecks(JMenu menu, Object eventSource) {
if(eventSource instanceof JButton){
JButton button = (JButton)eventSource;
Action action = button.getAction();
for(int i = 0 ; i < menu.getItemCount() ; ++i) {
JMenuItem item = menu.getItem(i);
item.setSelected(item.getAction() == action);
}
}
}
Directory "Sketcher 8 with toolbar buttons and menu icons"
Make sure you don't place it inside one of the inner classes inadvertently. The method first tests whether
or not the event originated with a JButton object. If it does then it's certainly from one of the color toolbar
buttons. You can then compare the Action object for the button with the Action object for each item in
the menu. The getItemCount() method for a JMenu object returns the number of menu items it contains.
You combine this with the getItem() method that returns the menu item at the index position specified by
the argument to access each of the menu items in turn. The setSelected() method for a menu item sets
the item as checked when the argument is true . This is only the case for the menu item that has the same
Action object as the button.
The actionPerformed() method for the TypeAction class is essentially the same as that for the Col-
orAction class:
public void actionPerformed(ActionEvent e) {
elementType = typeID;
setChecks(elementMenu, e.getSource());
}
Directory "Sketcher 8 with toolbar buttons and menu icons"
Just one extra statement. With these changes clicking a color or element type toolbar button updates the
menus.
ADDING MENU ICONS
Search WWH ::




Custom Search