Handle events for the menu bar and for the pop-up submenus. : Menu Event « Components « Flex






Handle events for the menu bar and for the pop-up submenus.

Handle events for the menu bar and for the pop-up submenus.
      
<!--
Code from Flex 4 Documentation "Using Adobe Flex 4".

This user guide is licensed for use under the terms of the Creative Commons Attribution 
Non-Commercial 3.0 License. 

This License allows users to copy, distribute, and transmit the user guide for noncommercial 
purposes only so long as 
  (1) proper attribution to Adobe is given as the owner of the user guide; and 
  (2) any reuse or distribution of the user guide contains a notice that use of the user guide is governed by these terms. 
The best way to provide notice is to include the following link. 
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

-->


    <!-- menus/MenuBarEventInfo.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"
    creationComplete="initCollections();">
    <fx:Script> 
         
        import mx.events.MenuEvent; 
        import mx.controls.Alert; 
        import mx.collections.*; 
        [Bindable] 
        public var menuBarCollection:XMLListCollection; 
        private var menubarXML:XMLList =<> 
            <menuitem label="Menu1"> 
            <menuitem label="MenuItem 1-A" data="1A"/> 
            <menuitem label="MenuItem 1-B" data="1B"/> 
            </menuitem> 
            <menuitem label="Menu2"> 
            <menuitem label="MenuItem 2-A" data="2A"/> 
            <menuitem label="MenuItem 2-B" data="2B"/> 
            </menuitem> 
            <menuitem label="Menu3" data="M3"/> 
            </> 
        // Event handler to initialize the MenuBar control. 
        private function initCollections():void { 
            menuBarCollection = new XMLListCollection(menubarXML); 
        } 
        // Event handler for the MenuBar control's change event. 
        private function changeHandler(event:MenuEvent):void { 
            // Only open the Alert for a selection in a pop-up submenu. 
            // The MenuEvent.menu property is null for a change event 
            // dispatched by the menu bar. 
            if (event.menu != null) { 
                Alert.show("Label: " + event.item.@label + "\n" + 
                    "Data: " + event.item.@data, "Clicked menu item"); 
            } 
        } 
        // Event handler for the MenuBar control's itemRollOver event. 
        private function rollOverHandler(event:MenuEvent):void { 
            rollOverTextArea.text = "type: " + event.type + "\n"; 
            rollOverTextArea.text += "target menuBarIndex: " + event.index + "\n"; 
        } 
        // Event handler for the MenuBar control's itemClick event. 
        private function itemClickHandler(event:MenuEvent):void { 
            itemClickTextArea.text = "type: " + event.type + "\n"; 
            itemClickTextArea.text += "target menuBarIndex: " + event.index + "\n"; 
        } 
      
    </fx:Script>
    <mx:Panel title="MenuBar Control Example" height="75%" width="75%"
        paddingTop="10" paddingLeft="10">
        <mx:Label width="100%" color="blue" text="Select a menu item." />
        <mx:MenuBar labelField="@label" dataProvider="{menuBarCollection}"
            change="changeHandler(event);" itemClick="itemClickHandler(event);"
            itemRollOver="rollOverHandler(event);" />
        <mx:TextArea id="rollOverTextArea" width="200" height="100" />
        <mx:TextArea id="itemClickTextArea" width="200" height="100" />
    </mx:Panel>
</s:Application>

   
    
    
    
    
    
  








Related examples in the same category

1.Get menu label from click eventGet menu label from click event
2.Menu event indexMenu event index
3.Menu with event listenerMenu with event listener
4.change event for MenuBarchange event for MenuBar
5.itemRollOver event for MenuBaritemRollOver event for MenuBar
6.Menu item click eventMenu item click event
7.Menu show eventMenu show event
8.Menu hide eventMenu hide event
9.Menu roll out eventMenu roll out event
10.Menu Item Roll Over eventMenu Item Roll Over event
11.Show the detailed information for MenuEvent
12.Create EventHandlers for Menu-Based ControlsCreate EventHandlers for Menu-Based Controls
13.A menu with a simple event listener.A menu with a simple event listener.
14.Using Menu control eventsUsing Menu control events
15.Menu labelFieldMenu labelField