Adobe AIR nested(submenu) menu items for context menu
February 21st, 2010
Hi, yesterday I was looking for fast answer on question “Can I have sub-menu for context menu in my adobe AIR application ?” Could not fast quick answer on 1st google page, with code example or explanation. It’s can be confusing cause actually Flex Web-based application can not implement context sub-menu, but in AIR application you can and it’s very simple! So I hope this post will be displayed on google 1st page when somebody will type similar question above.
So how to make it ?
First we need create our context menu:
var myContextMenu:ContextMenu = new ContextMenu();
Make our application use this contex menu:
this.contextMenu = myContextMenu;
Then we need to add some menu items for it:
var menuItem1:ContextMenuItem = new ContextMenuItem('Menu 1'); var menuItem2:ContextMenuItem = new ContextMenuItem('Menu 12);
Now will assign this items to our context menu:
// we should assign array of items to customItems property of the context menu myContextMenu.customItems = [menuItem1, menuItem2 ];
For now you’ll see context menu consists from two items on right click. Now we will add nested sub-menu.
Again we need to create context menu with items:
var subMenuItem1:ContextMenuItem = new ContextMenuItem('Sub-Menu 1'); var myContextSubMenu:ContextMenu = new ContextMenu(); myContextSubMenu.customItems = [subMenuItem1];
And now magic line that will assign our context sub-menu to one of main context-menu items:
menuItem1.submenu = myContextSubMenu;
So all about this property submenu that enable for AIR but not for Flex.
Btw to run some action when click for some menu item you can use code below:
subMenuItem1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemClick); private function menuItemClick(event:Event):void{ Alert.show('TADA'); }
You can download simple source code.



