ContextMenu in Adobe Flex (or, the right-click menu)

17 01 2008

Well, another wonder of Adobe is trying to modify the members of the right-click menu.

This is known as the ContextMenu. The example below is a modification from the Adobe Help files. It is interesting to note that

  1. You cannot just add the Sprite object directly to the Application; you must contain it within a Component; and
  2. If you do not do the hideBuiltInItems() call, then the Flash menu comes up on right click, instead of the Flex menu. (ie. you see Play, Loop, Quality, etc, rather than Settings, Debugger, etc).

Note that the Panel – myPanel – isn’t required. You can just add the newComponent to the Application via a sole addChild() call.

<?xml version=”1.0″ encoding=”utf-8″?>
initialize=”init(event)” xmlns:mx=”” layout=”absolute>

import mx.managers.PopUpManager;
import mx.core.UIComponent;

private var square:Sprite = new Sprite();

public function init(event:Event):void

var newComponent:UIComponent = new UIComponent();

//must set the UI component to wrap the sprite – so set width and height
newComponent.width = 100;

newComponent.height = 100;


var menuItem:ContextMenuItem = new ContextMenuItem(“Change Color”);

var customContextMenu:ContextMenu = new ContextMenu();

//hide the Flash menu


square.contextMenu = customContextMenu;


public function changeColor(event:ContextMenuEvent):void
{square.transform.colorTransform = getRandomColor();

public function getRandomColor():ColorTransform
return new ColorTransform(Math.random(), Math.random(), Math.random(),1,(Math.random() * 512) – 255, (Math.random() * 512) -255, (Math.random() * 512) – 255, 0);}



<mx:Panel id=”myPanel></mx:Panel>





5 responses

27 01 2008

Awesome! I’ve been trying to figure this out for a while.


3 10 2008
Lisa Pappas

Have you experimented with extending the popup so that it can be posted via keyboard only (for accessibility compliance)?

3 10 2008
Justin J. Moses


Do you mean opening up and navigating the context menu via the keyboard alone?

I’ll assume you did. I’ve been having a look but honestly can’t see how to manually dispatch an event to open the context menu. Unfortunately the “MouseEvent.RIGHT_CLICK” event type is only available via AIR.

I’m suspicious there must be a way deeper inside the flash.display.InteractiveObject class, as that is where the event originates…. I’ll keep looking.


25 12 2009
Tahir Alvi

but i want to know that if we add context menue for more then one component in air application from parent application?

28 10 2010

very nice Posts thank You

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: