3 Replies Latest reply on Jul 24, 2009 11:21 AM by Dharmesh Chheda

    Dynamic addition of  MXML Component

    Dharmesh Chheda

      Is it possible to add an MXML component through an ActionScript.


      I have a situation where I display multiple checkboxes and on click of each I show a subform. I am planning to make that subform as MXML component and add/remove it based on the user's checking/unchecking the checkbox..Is this possible?




        • 1. Re: Dynamic addition of  MXML Component
          BennyBeta Level 4

          You can use add/removeChild()...


          container.addChild( mxmlComponent );

          container.removeChild( mxmlComponent );


          Let me know if that helps...

          Ben Edwards

          • 2. Re: Dynamic addition of  MXML Component
            Dharmesh Chheda Level 1

            Hi Ben ..


                 Appreciate your quick response. This is my first Flex app. In a nut sheel here is a sample similar to what I am trying to achieve.


            Consider my Mxml component as follows



            <?xml version="1.0" encoding="utf-8"?>

            <!-- myComponents/TempComponent.mxml -->

            <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
                <mx:Button label="Hey there from component"/>



            Now here he my app mxml file


            <?xml version="1.0" encoding="utf-8"?>

            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

                        import mx.controls.Alert;
                        public function addRemove():void{
                            //How do i add that component to the Panel?


                 <mx:Checkbox id="cb" change="addRemove()" label="Check or Uncheck"/>

                 <mx:Panel  id="testPanel" x="0" y="0" width="100%" height="200" layout="absolute" borderThickness="0" borderThicknessBottom="0"/>





            Can you guide me what should go in the listener to add the component to the panel?

            • 3. Re: Dynamic addition of  MXML Component
              Dharmesh Chheda Level 1

              Hey Ben


                   I got how to instantiate the mxmlComponent. Its basically newing it up in the ActoinScript.


              Thanks for pointing me to that direction..