views:

187

answers:

5

//ActionScript code

import mx.controls.Menu; import flash.events.MouseEvent;

        private var menu12:Menu;

        private function init():void {
            menu12 = new Menu();
            menu12.labelField = "@label";
            menu12.dataProvider = xmlDP;
            menu12.showRoot = false;
            menu12.width = popUpButton.width;
            popUpButton.popUp = menu12;
        }

// XML Info

        <menu1 label="Some introduction" />
        <menu2 label="Disabled State (disabled)." enabled="false" />
        <sep1 type="separator" />
        <menu3 label="parent">
            <menu4 label="child1" />
        </menu3>
        <menu5 label="parent (disabled)" enabled="false">
            <menu6 label="child1" />
            <menu7 label="child2" />
            <menu8 label="child3" />
        </menu5>
        <menu9 type="separator" />
        <menu10 type="separator" />
        <menu11 id="leftButton" label="Left" type="radio" groupName="radioGroup" toggled="true" enabled="true"  />
        <menu12 id="rightButton" label="Right" type="radio" groupName="radioGroup" enabled="false" />
        <menu13 id="popupButton" label="Popup" type="radio" groupName="radioGroup" enabled="false" />

    </root>

// component

<mx:PopUpButton id="popUpButton"
            label="Please select an item"
            openAlways="true"
            creationComplete="init();" />

how to add addevents to the menus in the popupButton.

A: 

Add

menu12.addEventListener(MenuEvent.ITEM_CLICK,itemClickHandler);

at the end of your init function then add the listener function

private function itemClickHandler(event:MenuEvent):void{}

to your script.

slukse
A: 

menu12.addEventListener(MenuEvent.ITEM_CLICK,itemClickHandler);

This is for the menu, but i want to add to the nodes of each XML. for instance if i want to add an event for a particular node in xml... how can i do it.

A: 

I don't think you can put an event handler on a specific XML node the way you want. You'll have to look at the event coming in to be able to tell which handler should run. Take a look at Theo Hultberg's concept of guards to make the filtering code cleaner:

http://blog.iconara.net/2008/03/30/separating-event-handling-from-event-filtering/

macke
A: 

There must be some option to set the event to the particular item in the menu...

+1  A: 

Here's how I do it....

Add an attribute to each of the XML items to store an Event name for each Menu Item:

<menu4 label="child1" eventName="child1Event" />

And add an Event Listener for each known Event when the parent component is initialized:

private function init():void
{
    menu12 = new Menu();
    ...

    addEventListener("child1Event", child1EventHandler);
}

private function child1EventHandler(event:Event):void
{
    ....
}

(You could even parse the XML to add the event listeners dynamically

private function init():void
{
    menu12 = new Menu();
    ...

    addEventListener("child1Event", 
        function(event:Event)
        {
            alert(event.type);
        }
        );
}

Add an itemClick Event Handler:

private function popUpButton_itemClickHandler(event:MenuEvent):void
{
    if (event.item.@eventName != null)
        dispatchEvent(new Event(event.item.@eventName));
}

<mx:PopUpButton id="popUpButton"
        label="Please select an item"
        openAlways="true"
        creationComplete="init();" 
        itemClick="popUpButton_itemClickHandler(event);" />
Eric Belair