The Essential Guide to Flash CS4 AIR Development book is oriented to Flash developers interested in building desktop applications via Adobe AIR. You can preorder The Essential Guide to Flash CS4 AIR Development on Amazon or buy it on local bookstore starting from 22nd December.
In the first part of this article dedicated on AIR menus you've learned how to create an AIR native menu (read the full Creating AIR native menu with Flash CS4 - Part 1 article)
To create a submenu item, you use the NativeMenuItem class . Begin by opening the Ch06p01.as class and add the createFirstSubMenu() method , which creates an item of the NativeMenuItem class and adds two elements. This item will be used as a first submenu of the native menu you’re creating. Here’s the code:
private function createFirstSubMenu():NativeMenuItem
{
Next, create an instance of the NativeMenuItem class by showing the label that it needs to be associated with. Assign an instance of the NativeMenu class to the submenu property of the element you’ve just created. The native menu object associated with the submenu property will contain the subitems you’ll create for this element of the menu.
// create first submenu
var subMenu:NativeMenuItem =
new NativeMenuItem( "My first custom submenu" );
// initialize child container
subMenu.submenu = new NativeMenu();
Follow these steps to define the elements that will be displayed as children of the element of the menu you’ve just created:
1. Instantiate an object of the NativeMenuItem class by giving the constructor a label for the element.
2. Register an event listener method for the SELECT event (Event.SELECT). Each time the user selects this element from the menu, the menuItemSelected() method will
be executed.
3. Finally, add the element of the menu you’ve created to the submenu property of the submenu you’re creating with the addItem() method of the NativeMenuItem
class. Here’s the code:
// create first submenu child
var subMenuItem1:NativeMenuItem =
new NativeMenuItem( "menu 1 item 1" );
// register event listener for menu item
subMenuItem1.addEventListener( Event.SELECT, menuItemSelected );
// add item to submenu
subMenu.submenu.addItem( subMenuItem1 );
Create a second object of the NativeMenuItem class. Then register the
selection event of the element to the menuItemSelected() method .
Finally, add the element to the submenu you’re creating. Here’s the
code:
// create a second child, register event listener for
// selection event and assign to submenu
var subMenuItem2:NativeMenuItem =
new NativeMenuItem( "menu 1 item 2" );
subMenuItem2.addEventListener( Event.SELECT, menuItemSelected );
subMenu.submenu.addItem( subMenuItem2 );
Next, return the menu you’ve created so that it can be added as a child of the main context menu.
return subMenu;
}
The first submenu has been created and is ready to use. Before testing
your application, you have to define the createSecondSubMenu() method ,
which will deal with initializing the second submenu of the context
menu you’re creating.