Programmatically opening a Spark DropDownList control in Flex 4

In a previous example, “Determining if a drop down menu is open in a Spark DropDownList control in Flex 4″, we saw how we could check whether a drop down list menu is open in a Spark DropDownList control in Flex 4 by checking the Boolean isDropDownOpen property.

The following example shows how you can programmatically open a Spark DropDownList control in Flex 4 by calling the openDropDown() method.

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 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/2009/08/07/programmatically-opening-a-spark-dropdownlist-control-in-flex-4/ -->
<s:Application name="Spark_DropDownList_openDropDown_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">
 
    <fx:Script>
        <![CDATA[
            protected function ddl_rollOver(evt:MouseEvent):void {
                ddl.openDropDown();
            }
        ]]>
    </fx:Script>
 
    <s:DropDownList id="ddl"
            requireSelection="true"
            width="150"
            horizontalCenter="0"
            top="40"
            rollOver="ddl_rollOver(event);">
        <s:dataProvider>
            <s:ArrayList source="[The,Quick,Orange,Fox,Jumps,Over,The,Lazy,Dog]" />
        </s:dataProvider>
    </s:DropDownList>
 
</s:Application>

View source is enabled in the following example.

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/2009/08/07/programmatically-opening-a-spark-dropdownlist-control-in-flex-4/ -->
<s:Application name="Spark_DropDownList_openDropDown_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.collections.ArrayList;
            import spark.components.DropDownList;
 
            private var ddl:DropDownList;
 
            private function init():void {
                ddl = new DropDownList();
                ddl.dataProvider = new ArrayList(["The", "Quick", "Orange", "Fox", "Jumps", "Over", "The", "Lazy", "Dog"]);
                ddl.requireSelection = true;
                ddl.width = 150;
                ddl.horizontalCenter = 0;
                ddl.top = 40;
                ddl.addEventListener(MouseEvent.ROLL_OVER, ddl_rollOver);
                addElement(ddl);
            }
 
            protected function ddl_rollOver(evt:MouseEvent):void {
                ddl.openDropDown();
            }
        ]]>
    </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.

2 thoughts on “Programmatically opening a Spark DropDownList control in Flex 4

    1. @MiMee,

      You can use the closeDropDown() method.

      <s:HGroup>
          <s:Button rollOver="ddl.openDropDown();" />
          <s:Button rollOver="ddl.closeDropDown(true);" />
      </s:HGroup>
       
      <s:DropDownList id="ddl" labelField="fontName">
          <s:dataProvider>
              <s:ArrayList source="{Font.enumerateFonts(true).sortOn('fontName')}" />
          </s:dataProvider>
      </s:DropDownList>

      Peter

Comments are closed.