Setting a custom context menu on a Spark List control item renderer in Flex 4

The following example shows how you can set a custom context menu on a Spark List control item renderer in Flex 4 by setting the contextMenu property in the item renderer’s dataChange event.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see To download the latest nightly build of the Flex 4 SDK, see

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

<?xml version="1.0"?>
<!-- -->
<s:Application name="Spark_List_itemRenderer_contextMenu_test"
            import mx.utils.StringUtil;
            protected function lst_labelFunc(data:Object):String {
                return StringUtil.substitute("[{0}]\t{1}", data.symbol,;
    <s:List id="lst"
            horizontalCenter="0" verticalCenter="0">
            <s:VerticalLayout gap="0"
                    requestedRowCount="5" />
                <fx:Object symbol="ADBE" name="Adobe Systems Incorporated" price="32.49" isNew="true" />
                <fx:Object symbol="AAPL" name="Apple Inc." price="246.25" isNew="false" />
                <fx:Object symbol="GOOG" name="Google Inc." price="498.67" isNew="true" />
                <fx:Object symbol="HPQ" name="Hewlett-Packard Company" price="48.33" isNew="false" />
                <fx:Object symbol="IBM" name="International Business Machines Corp." price="123.92" isNew="false" />
                <fx:Object symbol="NVDA" name="NVIDIA Corporation" price="14.20" isNew="true" />

And the custom Spark List control item renderer, skins/CustomColumnItemRen.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<s:ItemRenderer name="CustomColumnItemRen"
        <s:State name="normal" />
        <s:State name="hovered" />
        <s:State name="selected" />
        <s:HorizontalLayout gap="0"
                paddingLeft="3" paddingRight="3"
                paddingTop="3" paddingBottom="3"
                verticalAlign="middle" />
            import mx.controls.Alert;
            import mx.formatters.CurrencyFormatter;
            protected const ICON:Class;
            protected function dataChangeHandler(evt:Event):void {
                const currFmt:CurrencyFormatter = new CurrencyFormatter();
                currFmt.precision = 2;
                priceTxt.text = currFmt.format(data.price);
                if (data.isNew) {
                    bmpImg.source = ICON;
                // Create custom context menu. 
                const cmi:ContextMenuItem = new ContextMenuItem("Copy quote");
                cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, cmi_menuItemSelect);
                const cm:ContextMenu = new ContextMenu();
                cm.customItems = [cmi];
                contextMenu = cm;
            protected function cmi_menuItemSelect(evt:ContextMenuEvent):void {
                System.setClipboard(labelDisplay.text + "\t" + priceTxt.text);
    <s:Label id="labelDisplay" maxDisplayedLines="1" showTruncationTip="true" width="100%" />
    <s:Label id="priceTxt" textAlign="right" fontWeight="bold" />
    <s:BitmapImage id="bmpImg" width="16" height="16" />


View source is enabled in the following example.


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.

3 thoughts on “Setting a custom context menu on a Spark List control item renderer in Flex 4

  1. If I insert this spark list in a Panel (Spark) right-click does not work.

    wont work :-/ Any clue why?

Comments are closed.