Adding custom context menu items to a Flex 4 application

In a previous example, “Adding custom context menu items to a Flex application”, we saw you could add custom context menu items to a Flex application by creating new ContextMenuItem objects and adding them to the Flex application’s customItems array (via the contextMenu property).

The following example shows how you can add custom context menu items to a Flex 4 application by creating new ContextMenuItem objects and adding them to the Flex application’s customItems array (via the contextMenu property, and using the FlexGlobals.topLevelApplication property instead of the application property).

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/18/adding-custom-context-menu-items-to-a-flex-4-application/ -->
<s:Application name="Spark_Application_contextMenu_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
 
            private function init():void {
                var customMenuItem1:ContextMenuItem = new ContextMenuItem("Flex SDK " + mx_internal::VERSION, false, false);
                var customMenuItem2:ContextMenuItem = new ContextMenuItem("Player " + Capabilities.version, false, false);
                var contextMenuCustomItems:Array = FlexGlobals.topLevelApplication.contextMenu.customItems;
                contextMenuCustomItems.push(customMenuItem1);
                contextMenuCustomItems.push(customMenuItem2);
            }
        ]]>
    </fx:Script>
 
    <s:Label text="Right click to see custom context menu items."
             horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

6 thoughts on “Adding custom context menu items to a Flex 4 application

    1. @MechanisM,

      I’m no locale expert, but maybe look into the ResourceManager documentation. You could probably do a bunch of locale specific strings and then just load the context menu strings from the correct resource properties file whatever.

      As for FlashVars or XML, it’s the same as the example above, except you’d load an XML file at runtime and set the strings, or grab them from FlashVars. There are various XML/FlashVars examples on this site or in the official documentation.

      Peter

  1. Is it possible to add a contextmenu to a component inside a component (and possible even ‘deeper’)?
    Because now the right-click event is not explicit on one component but its whole application.

    Thank you for the information!

  2. Hello,

    There seems to be a bug, this doesn’t work on new spark title windows?

    Regards,
    Vijay

  3. I want to open a menu and sub menu by right clicking and i dont want the last 4 by default options that comes like print settings global settings please give me a solution

Comments are closed.