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.
This is the thrid part of the series dedicated to AIR menus (Chapter 6 of the Essential Guide to Flash CS4 AIR Development). You can read the previous articles here:
- Creating AIR submenus for a native menu with Flash CS4 - Part 2 (The Essential Guide to Flash CS4 AIR Development Highlight)
- Creating AIR native menu with Flash CS4 - Part 1 (The Essential Guide to Flash CS4 AIR Development Highlight)
Next, you’ll employ the menuItemSelected() method , which will be launched each time the user selects one of the items on the context menu. When a selection event is generated, the event listener method receives an instance of the flash.events.Event class as an argument. The selection events are transmitted by the selected element on the menu, through the hierarchical structure of the menu, to the root element.
The object received as an argument has the two following properties:
- target
- currentTarget
If the event has been registered directly on the elements of the menu (like in this case), the two properties will have the same value. In this case, both properties refer to the instance of the selected NativeMenuItem class . However, if the event is registered on the root menu or on one of the roots of the submenus, the target property of the received event object will always refer to the selected element. The currentTarget property will always refer to the object the event is registered on.
When an item is selected, the menuItemSelected() method writes the label of the selected element in the TextArea, which acts as a text output console. Here’s the code:
// called on click on menu items
private function menuItemSelected( evt : Event ):void
{
// access NativeMenuItem instance selected
var item:NativeMenuItem = evt.target as NativeMenuItem;
// write in the textarea selected item's label
output.appendText( "CLICKED ON: " + item.label + File.lineEnding );
}
Displaying the code for the native submenus
This is the complete Ch06p01.as class that you have created:
package com.comtaste.foed.essentialair.chapter6
{
import fl.controls.Button;
import fl.controls.TextArea;
import flash.display.MovieClip;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.Event;
import flash.filesystem.File;
public class Ch06p01 extends MovieClip
{
// onstage components
public var button:Button;
public var output:TextArea;
// class properties
private var menuRoot:NativeMenu;
public function Ch06p01()
{
super();
// generate native menu to use
createNativeMenu();
// assign menu to right- click on button
button.contextMenu = menuRoot;
}
// create a complete native menu
private function createNativeMenu():void
{
// instantiate main menu object
menuRoot = new NativeMenu();
// append subMenus to menu root
menuRoot.addItem( createFirstSubMenu() );
menuRoot.addItem( createSecondSubMenu() );
}
private function createFirstSubMenu():NativeMenuItem
{
// create first submenu
var subMenu:NativeMenuItem =
new NativeMenuItem( "My first custom submenu" );
// initialize child container
subMenu.submenu = new NativeMenu();
// 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 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 );
return subMenu;
}
private function createSecondSubMenu():NativeMenuItem
{
// create first submenu
var subMenu:NativeMenuItem =
new NativeMenuItem( "Second submenu" );
// initialize child container
subMenu.submenu = new NativeMenu();
// create first submenu child
var subMenuItem1:NativeMenuItem =
new NativeMenuItem( "menu 2 item 1" );
// register event listener for menu item
subMenuItem1.addEventListener( Event.SELECT,
menuItemSelected );
// add item to submenu
subMenu.submenu.addItem( subMenuItem1 );
// add a separator item
// label will be ignored for separator items
var subMenuSeparator:NativeMenuItem =
new NativeMenuItem( "", true );
// add separator to menu
subMenu.submenu.addItem( subMenuSeparator );
// create a second child, register event listener for
// selection event and assign to submenu
var subMenuItem2:NativeMenuItem =
new NativeMenuItem( "menu 2 item 2" );
subMenuItem2.addEventListener( Event.SELECT,
menuItemSelected );
subMenu.submenu.addItem( subMenuItem2 );
// create a new item as an internal submenu
// using addSubmenu command
var childSubMenu:NativeMenuItem =
subMenu.submenu.addSubmenu( new NativeMenu(),
"Nested menu" );
// initialize child container
childSubMenu.submenu = new NativeMenu();
// create a child, register event listener for
// selection event and assign to internal submenu
var subMenuItem3:NativeMenuItem =
new NativeMenuItem( "menu 2 nested item 1" );
subMenuItem3.addEventListener( Event.SELECT, menuItemSelected );
childSubMenu.submenu.addItem( subMenuItem3 );
return subMenu;
}
// called on click
private function menuItemSelected( evt : Event ):void
{
var item:NativeMenuItem = evt.target as NativeMenuItem;
output.appendText( "CLICKED ON: "
+ item.label + File.lineEnding );
}
} // close class
} // close package
Testing the native menus
Now the application is ready to be tested. To test it, go back to the Flash ch06p01.fla. project . Then run the application by selecting the Test Movie command from the Flash CS4 Controls menu. Once it has been compiled and executed, right- click (or on Mac OS X systems, Ctrl- click) the button at the center of the stage. The native menu you’ve prepared will appear. You can see the application in Figure 6-4.
Figure 6-4. The ch06p01.fla project in Flash
Each time you select an item from the context menu, the label of the
selected element will be displayed in the TextArea. In the next
section, you’ll learn how to use window- and application- level menus
in real- world applications.
Thank you very much for sharing such cool things. Really good~ I would like to share something with you as well.I promise that you can find your favourite will be your faithful reader.Thanks for taking the time to talk about this, I feel fervently about this and I take pleasure in learning about this topic.
Posted by: ritaboye | January 04, 2011 at 09:38 AM
When you do article marketing, your intention is to increase the number of visitors to your website. It is not our abilities that show what we truly are, it is our choices. I have look for such a article for a long time, thanks a lot. http://www.uggsclassicboot.com/
Posted by: uggs outlet | December 03, 2010 at 08:56 AM
good tutorial that is unique well done
Posted by: أبل | November 21, 2010 at 04:12 PM