3 Replies Latest reply on Jan 10, 2008 11:19 PM by eodC

    Dynamic Creation of Components

      I want to create components at runtime depending on the list provided. When I click the component from the list, that particular component should be displayed. Please provide an idea how to Complete it. As of now I am creating the each and every component manually using the switch case. Please provide an alternative.
        • 1. Re: Dynamic Creation of Components
          chris.huston.t10 Level 3
          Here is one possible way to create components, though there might be more elegant solutions.

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          import mx.events.ListEvent;
          import mx.controls.CheckBox;
          import mx.controls.Button;
          import mx.controls.List;
          import mx.collections.ArrayCollection;

          private var newList:List = new List();
          private var newButton:Button = new Button();
          private var newCheckBox:CheckBox = new CheckBox();

          [Bindable] private var componentsAC:ArrayCollection = new ArrayCollection([
          {compType:newList, compName:'List'},
          {compType:newButton, compName:'Button'},
          {compType:newCheckBox, compName:'CheckBox'}]);

          private function addComponent(evt:Event):void {
          myVBox.addChild(new evt.currentTarget.selectedItem.compType.constructor);

          <mx:List x="10" y="10" height="222" click="addComponent(event)" dataProvider="{componentsAC}" labelField="compName"></mx:List>
          <mx:VBox x="180" y="10" height="100%" id="myVBox">


          I hope this helps.

          • 2. Re: Dynamic Creation of Components
            keep in mind i think you can do something along the lines of

            var myclass:Class = List;
            var myList:List = myclass();

            there's also ClassFactory which are used a lot to instantiate things like ItemRenderers, but generally, they create instances of classes.

            • 3. Re: Dynamic Creation of Components
              eodC Level 1
              Hi Vygo,

              Thanks for sending the answer very soon.