Resizing a Spark DropDownList control to match the currently selected item in Flex 4

The following example shows how you can resize the Spark DropDownList control to match the currently selected list item in Flex 4 by setting the DropDownList control’s typicalItem property to the currently selected item.

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/2010/01/21/resizing-a-spark-dropdownlist-control-to-match-the-currently-selected-item-in-flex-4/ -->
<s:Application name="Spark_DropDownList_typicalItem_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <s:DropDownList id="cb"
                    labelField="name"
                    typicalItem="{cb.selectedItem}"
                    requireSelection="true"
                    left="20" top="20">
        <s:dataProvider>
            <s:ArrayList>
                <fx:Object name="Baltimore Orioles" abbr="BAL" />
                <fx:Object name="Boston Red Sox" abbr="BOS" />
                <fx:Object name="Chicago White Sox" abbr="CWS" />
                <fx:Object name="Cleveland Indians" abbr="CLE" />
                <fx:Object name="Detroit Tigers" abbr="DET" />
                <fx:Object name="Kansas City Royals" abbr="KC" />
                <fx:Object name="Los Angeles Angels of Anaheim" abbr="LAA" />
                <fx:Object name="Minnesota Twins" abbr="MIN" />
                <fx:Object name="New York Yankees" abbr="NYY" />
                <fx:Object name="Oakland Athletics" abbr="OAK" />
                <fx:Object name="Seattle Mariners" abbr="SEA" />
                <fx:Object name="Tampa Bay Devil Rays" abbr="TB" />
                <fx:Object name="Texas Rangers" abbr="TEX" />
                <fx:Object name="Toronto Blue Jays" abbr="TOR" />
            </s:ArrayList>
        </s:dataProvider>
    </s:DropDownList>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the typicalItem property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/01/21/resizing-a-spark-dropdownlist-control-to-match-the-currently-selected-item-in-flex-4/ -->
<s:Application name="Spark_DropDownList_typicalItem_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Script>
        <![CDATA[
            import spark.events.IndexChangeEvent;
 
            protected function cb_changeHandler(evt:IndexChangeEvent):void {
                cb.typicalItem = cb.selectedItem;
            }
        ]]>
    </fx:Script>
 
    <s:DropDownList id="cb"
            labelField="name"
            requireSelection="true"
            left="20" top="20"
            change="cb_changeHandler(event);"
            creationComplete="cb.typicalItem = cb.selectedItem;">
        <s:dataProvider>
            <s:ArrayList>
                <fx:Object name="Baltimore Orioles" abbr="BAL" />
                <fx:Object name="Boston Red Sox" abbr="BOS" />
                <fx:Object name="Chicago White Sox" abbr="CWS" />
                <fx:Object name="Cleveland Indians" abbr="CLE" />
                <fx:Object name="Detroit Tigers" abbr="DET" />
                <fx:Object name="Kansas City Royals" abbr="KC" />
                <fx:Object name="Los Angeles Angels of Anaheim" abbr="LAA" />
                <fx:Object name="Minnesota Twins" abbr="MIN" />
                <fx:Object name="New York Yankees" abbr="NYY" />
                <fx:Object name="Oakland Athletics" abbr="OAK" />
                <fx:Object name="Seattle Mariners" abbr="SEA" />
                <fx:Object name="Tampa Bay Devil Rays" abbr="TB" />
                <fx:Object name="Texas Rangers" abbr="TEX" />
                <fx:Object name="Toronto Blue Jays" abbr="TOR" />
            </s:ArrayList>
        </s:dataProvider>
    </s:DropDownList>
 
</s:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/01/21/resizing-a-spark-dropdownlist-control-to-match-the-currently-selected-item-in-flex-4/ -->
<s:Application name="Spark_DropDownList_typicalItem_test"
               xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayList;
            import mx.events.FlexEvent;
            import mx.events.IndexChangedEvent;
            import spark.components.DropDownList;
            import spark.events.IndexChangeEvent;
 
            protected var cb:DropDownList;
            protected var arrList:ArrayList;
 
            protected function init():void {
                var arr:Array = [];
                arr.push({name:"Baltimore Orioles", abbr:"BAL"});
                arr.push({name:"Boston Red Sox", abbr:"BOS"});
                arr.push({name:"Chicago White Sox", abbr:"CWS"});
                arr.push({name:"Cleveland Indians", abbr:"CLE"});
                arr.push({name:"Detroit Tigers", abbr:"DET"});
                arr.push({name:"Kansas City Royals", abbr:"KC"});
                arr.push({name:"Los Angeles Angels of Anaheim", abbr:"LAA"});
                arr.push({name:"Minnesota Twins", abbr:"MIN"});
                arr.push({name:"New York Yankees", abbr:"NYY"});
                arr.push({name:"Oakland Athletics", abbr:"OAK"});
                arr.push({name:"Seattle Mariners", abbr:"SEA"});
                arr.push({name:"Tampa Bay Devil Rays", abbr:"TB"});
                arr.push({name:"Texas Rangers", abbr:"TEX"});
                arr.push({name:"Toronto Blue Jays", abbr:"TOR"});
                arrList = new ArrayList(arr);
 
                cb = new DropDownList();
                cb.labelField = "name";
                cb.requireSelection = true;
                cb.dataProvider = arrList;
                cb.left = 20;
                cb.top = 20;
                cb.addEventListener(IndexChangeEvent.CHANGE, cb_changeHandler);
                cb.addEventListener(FlexEvent.CREATION_COMPLETE, cb_creationCompleteHandler);
                addElement(cb);
            }
 
            protected function cb_changeHandler(evt:IndexChangeEvent):void {
                cb.typicalItem = cb.selectedItem;
            }
 
            protected function cb_creationCompleteHandler(evt:FlexEvent):void {
                cb.typicalItem = cb.selectedItem;
            }
        ]]>
    </fx:Script>
 
</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.