Flex: Adding Your Own Context Menu Items

Well I've been working with Flex alot more so I will hopefully be posting more things on Flex. Something I have been working on lately is adding Context Menu items to my applications. You can add as many as you'd like but I would limit them to only the essential ones. I searched online and found various examples. Here is how I did it:

FlexContextMenu.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">

   <mx:Script>
      <![CDATA[
         [Bindable] public var cm:ContextMenu;
         
         private function init():void
         {
            // Create custom context menu items
            var cm1:ContextMenuItem = new ContextMenuItem("Copyright © 2008 DCFusion.com");
            
            cm1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,cm1_link);
            
            cm = new ContextMenu();
            cm.hideBuiltInItems();
            cm.customItems = [cm1];
            
            application.contextMenu = cm;
         }
         
         private function cm1_link(evt:ContextMenuEvent):void
         {
            goToURL("http://www.dcfusion.com/blog/");
         }
         
         public function goToURL(urlStr:String):void
         {
            var webPageURL:URLRequest = new URLRequest(urlStr);
            navigateToURL(webPageURL, '_blank');
         }
      ]]>
   </mx:Script>
   
   <mx:Panel width="100%" height="100%" title="Flex Context Menu Example">
      <mx:Label text="Welcome to my Flex app!"/>
   </mx:Panel>
   
</mx:Application>

I added a link to my new context menu item using an event listener, but that is not necessary.

And the final product (right-click on the Flex app to see the context menu):

No comments yet.

(will not be published)
Leave this field empty: