Search code examples
apache-flexdynamiccomboboxmxml

MXML - Create combobox dynamically on click of button


Need help on creating combo box dynamically in Flex. How do I create combobox dynamically on click of button.

Thanks in advance.


Solution

  • For example:

    public function createComboBox_clickHandler(event:Event):void {
                var myComboBox:ComboBox = new ComboBox();
                var comboBoxDataProvider:ArrayCollection =new ArrayCollection([
                    { name: "box1", value: "value1"},
                    { name: "box2", value: "value2"},
                    { name: "box3", value: "value3"},
                    { name: "box4", value: "value4"}
                ]);
    
                myComboBox.x = 100;
                myComboBox.y = 100;
                myComboBox.dataProvider = comboBoxDataProvider;
                myComboBox.labelField = "name";
                myComboBox.addEventListener(ListEvent.CHANGE, myComboBox_ClickHandler);
                container.addElement(myComboBox);
            }
    
            public function myComboBox_ClickHandler(event:ListEvent):void{
                trace(event.currentTarget.selectedItem.value);
            }
    

    And Button for the click (and a container for them both)

    <s:BorderContainer id="container" width="100%" height="100%">
        <s:Button id="createComboBoxButton" click="createComboBox_clickHandler(event)" label="Create a combobox dynamically"/>
    </s:BorderContainer>