The following example shows how you can determine when the selected item changes in a Spark DropDownList control in Flex 4 by listening for the change event.

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"?>
<!-- http://blog.flexexamples.com/2009/04/09/determining-when-the-selected-item-changes-in-a-spark-dropdownlist-control-in-flex-gumbo/ -->
<s:Application name="Spark_DropDownList_change_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 dropDownList_changeHandler(evt:IndexChangeEvent):void {
                lbl.text = "You selected " + dropDownList.selectedItem.team;
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:ArrayList id="arrList">
            <fx:Object team="Los Angeles" name="Angels of Anaheim" />
            <fx:Object team="Seattle" name="Mariners" />
            <fx:Object team="Oakland" name="Athletics" />
            <fx:Object team="Texas" name="Rangers" />
            <fx:Object team="Cleveland" name="Indians" />
            <fx:Object team="Detroit" name="Tigers" />
            <fx:Object team="Minnesota" name="Twins" />
            <fx:Object team="Chicago" name="White Sox" />
            <fx:Object team="Kansas City" name="Royals" />
            <fx:Object team="Boston" name="Red Sox" />
            <fx:Object team="New York" name="Yankees" />
            <fx:Object team="Toronto" name="Blue Jays" />
            <fx:Object team="Baltimore" name="Orioles" />
            <fx:Object team="Tampa Bay" name="Rays" />
            <fx:Object team="Arizona" name="Diamondbacks" />
            <fx:Object team="Colorado" name="Rockies" />
            <fx:Object team="San Diego" name="Padres" />
            <fx:Object team="Los Angeles" name="Dodgers" />
            <fx:Object team="San Francisco" name="Giants" />
            <fx:Object team="Chicago" name="Cubs" />
            <fx:Object team="Milwaukee" name="Brewers" />
            <fx:Object team="St. Louis" name="Cardinals" />
            <fx:Object team="Houston" name="Astros" />
            <fx:Object team="Cincinnati" name="Reds" />
            <fx:Object team="Pittsburgh" name="Pirates" />
            <fx:Object team="Philadelphia" name="Phillies" />
            <fx:Object team="New York" name="Mets" />
            <fx:Object team="Atlanta" name="Braves" />
            <fx:Object team="Washington" name="Nationals" />
            <fx:Object team="Florida" name="Marlins" />
        </s:ArrayList>
    </fx:Declarations>
 
    <s:VGroup horizontalCenter="0" top="10">
        <s:DropDownList id="dropDownList"
                dataProvider="{arrList}"
                labelField="team"
                prompt="Please select a team..."
                change="dropDownList_changeHandler(event);" />
        <s:Label id="lbl" />
    </s:VGroup>
 
</s:Application>

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.

 
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.

2 Responses to Determining when the selected item changes in a Spark DropDownList control in Flex 4

  1. will says:

    how can i use this code to goto a url on change event?

  2. sathish says:

    hiiii how to select an item in dropdownbox.
    in case we want to preselect based on item not index.
    for eg:in the above example if we want to preselect item ‘Boston’ and if we don’t know the index .

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