07
Nov
08

Adding custom context menu items to a Flex application

The following example shows how you can 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).

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/07/adding-custom-context-menu-items-to-a-flex-application/ -->
<mx:Application name="Application_contextMenu_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            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 = application.contextMenu.customItems;
                contextMenuCustomItems.push(customMenuItem1);
                contextMenuCustomItems.push(customMenuItem2);
            }
        ]]>
    </mx:Script>

    <mx:Label text="Right click to see custom context menu items." />

</mx:Application>

View source is enabled in the following example.


5 Responses to “Adding custom context menu items to a Flex application”


  1. 1 Ram Nov 7th, 2008 at 5:41 pm

    Could not see any of the GUMBO examples. “It says download
    error. Try to download again?” I have Flash 10 installed in
    my computer. I am using a XP machine.

  2. 2 peterd Nov 7th, 2008 at 6:33 pm

    Ram,

    This isn’t a Gumbo example (and should only require Flash Player 9.0.115.0), but what version of Flash Player 10 are you using? http://blog.flexexamples.com/about-you/

    Does this happen in all browsers, or just one?

    Peter

  3. 3 Raza Nov 8th, 2008 at 5:40 pm

    Very Nice Example
    I extended your example by adding an event listener and navigating to a webpage!
    It works fine, but when i do right click on a swf loader, it doesnt navigate to webpage (seems like it simply bypass the event)

    Could you please find the solution of this problem?

    My code is here

    //in initApp
    mx.core.Application.application.contextMenu.hideBuiltInItems();
    var mnuCRight:ContextMenuItem = new ContextMenuItem(”Copyright © 2008″, true);
    mnuCRight.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, ctMenu);
    mx.core.Application.application.contextMenu.customItems.push(mnuCRight);

    //function
    private function ctMenu(event:ContextMenuEvent):void
    {
    navigateToURL(new URLRequest(”www.myweb.com”), “_blank”);
    }

  4. 4 Massic Jan 22nd, 2009 at 2:07 am

    Nice, but dont let your immagination be kissed by the idea to add “select all” menu item. You’ll never see it :( Maybe its a “reserved” label …

  5. 5 Peter deHaan Jan 22nd, 2009 at 8:38 am

    Massic,

    Correct, “Select All” is a reserved/restricted word. For a complete list, see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/ui/ContextMenuItem.html

    The following captions are not allowed, but the words may be used in conjunction with other words to form a custom caption (for example, although “Paste” is not allowed, “Paste tastes great” is allowed):

    • Save
    • Zoom In
    • Zoom Out
    • 100%
    • Show All
    • Quality
    • Play
    • Loop
    • Rewind
    • Forward
    • Back
    • Movie not loaded
    • About
    • Print
    • Show Redraw Regions
    • Debugger
    • Undo
    • Cut
    • Copy
    • Paste
    • Delete
    • Select All
    • Open
    • Open in new window
    • Copy link

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed