in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

macromedia flash mx - Reinhardt R.

Reinhardt R., Lott J macromedia flash mx - John Wiley & Sons, 2004. - 987 p.
ISBN 0-7645-4354-7
Download (direct link): macromediaflash2004.pdf
Previous << 1 .. 322 323 324 325 326 327 < 328 > 329 330 331 332 333 334 .. 419 >> Next

import mx.controls.Menu; var cmEdit:Menu = Menu.createMenu(); var xmlMenuData:XML = new XML(); xmlMenuData.onLoad = function():Void { cmEdit.dataProvider = this.firstChild;
xmlMenuData.ignoreWhite = true; xmlMenuData.load("menu.xml");
Notice that the data in the XML file has a root element of <menu> to ensure that it is well formed (well-formed XML data has a single root element per document). When the data is loaded, the firstChild of the XML object is assigned to the dataProvider instead of the XML object itself.
720 Part VIII ¦ Using Components
Removing Items
You can remove menu items with the following three methods:
¦ removeMenuItem(): This method requires you pass it a reference to one of the items. You can get references to menu items in several ways, as you’ll see in subsequent sections. Alternatively, if this method is invoked directly from a menu item, it requires no parameters, and it removes the item from which it is called.
¦ removeMenuItemAt(): This method removes an item given the index.
¦ removeAll(): This methods removes all items in a menu or menu item.
Showing and Hiding Menu Instances
Up this point you’ve seen how to create Menu i nstances, but not how to actually show them. The default setting for a menu is that it is hidden. You can show a menu by calling the show() method. The show() method requires two parameters — the x and y coordinates at which to display the menu. For example, the following code will show a menu at 100,100., 100);
Most often, Menu i nstances are associated with Button component instances such that they display when the button has been clicked. Therefore, you typically call the show() method to display the menu just above or below the Button instance. For example:
var oListener:Object = new Object(); = cmEdit; = function(oEvent:Object):Void { var nX:Number =;
var nY:Number = +;, nY);
cbtEdit.addEventListener("click", oListener);
By default, Menu i nstances disappear once a selection has been made. Therefore you don’t often need to programmatically hide the instance. However, should you want to, you can hide the menu with the hide() method.
Handling Menu Events
Menu component instances dispatch the following events:
¦ show: Dispatched when the instance is shown
¦ hide: Dispatched when the instance is hidden
¦ rollOver: Dispatched when the user rolls over an item
¦ rollOut: Dispatched when the user rolls out of an item
¦ change: Dispatched when the user selects an item
The rollOver, rollOut, and change Event objects include an additional property not included in the show and hide Event objects. The property, called menultem, is a reference to the menu item that initiated the event.
Chapter 28 ¦ Using V2 UI Components 721
Changing Items
You can alter menu items in two ways — setting the enabled state or selected state.
Regardless of what kind of changes you want to make to a menu item, however, you first need to get a reference to the menu item. If you are making the changes within an event handler method such as rollOver(), rollOut(), or change(), the Event object’s menuItem property is a reference to the menu item that initialized the event. If, within an event handler method, you want to get a reference to a menu item other than the item that initialized the event, you can obtain the reference as one of the nested items of the Menu instance that is references by the Event object’s target property.
There are essentially two ways to get a reference to a menu item from a Menu i nstance (or from a nested menu item). You can use the getMenuItemAt() method to get an item by index. Or you can get a reference by name if you gave the menu item an instance name when you created it. (You can give an item an instance name via the i nstanceName property/attribute.)
Each menu item is an instance of the MenuDataProvider class. The MenuDataProvider class looks very much like the XMLNode class. In fact, it has the same properties such as
attributes and childNodes.
If you want to set the enabled or selected state of a menu item, the best way to do that is to use the setMenuItemEnabled() or setMenuItemSelected() methods. These methods set the state and then also automatically update the view. Both methods require you pass them a reference to the menu item, and then a Boolean value indicating the state to which you want to set the item. For example, the following disables a Menu item with an instance name of undo:
cmEdit.setMenuItemEnabled(cmEdit.undo, false);
The following example shows how you can create a Button and Menu i tem such that the paste option of the Menu item is enabled following the copy option being selected. Then, once paste is selected, it becomes disabled again.
import mx.controls.Menu; var cmEdit = Menu.createMenu(); var xmlMenuData:XML = new XML(); xmlMenuData.onLoad = function():Void { cmEdit.dataProvider = this.firstChild;
Previous << 1 .. 322 323 324 325 326 327 < 328 > 329 330 331 332 333 334 .. 419 >> Next