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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

6 Responses to Adding custom context menu items to a Flex 4 application

  1. MechanisM says:

    Thank You Peter!!
    How to add context menu depending on Locales?
    or From FlashVars or via XML?

    • Peter deHaan says:

      @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

  2. Anonymous says:

    is there a way to listen right-click event in Flash builder 4?

  3. Dieter says:

    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!

  4. Vijayanto says:

    Hello,

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

    Regards,
    Vijay

  5. abhishek dutta says:

    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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree