2 Replies Latest reply on Aug 19, 2010 12:33 AM by BhaskerChari

    dynamic label creation?

    welcomecan Level 1

      hai friends,

               i reterive one array data from db. i want to create label dynamically depend that array length.  give any example.

       

       

      suppose i have taken that array length is 5. i want to display 5 labels vertically

        • 1. Re: dynamic label creation?
          ShardulSingh Level 3

          Hi,

          You can do it as below.Let me know if you have any issue.

           

           

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
              creationComplete="onCreationComplete()">
          <mx:Script>
              <![CDATA[
                  import mx.controls.Label;
                  private function onCreationComplete():void
                  {
                      var arrFromServerSide : Array = ["Name","age","City","State","Country"];
                      for each (var arg : String in arrFromServerSide)
                      {
                          var lbl : Label = new Label();
                          lbl.width = 130;
                          lbl.height = 27;
                          lbl.text = arg;
                          lblContainer.addChild(lbl);
                      }
                  }
              ]]>
          </mx:Script>
              <mx:VBox id="lblContainer" width="180" height="150" verticalGap="2"/>
          </mx:Application>

           

           

           

           

           

           

           

           

           

           

           

          with Regards,

           

          Shardul Singh Bartwal

          • 2. Re: dynamic label creation?
            BhaskerChari Level 4

            Hi WelcomeCan,

             

            Here is the simple example as how to add the Labels dynamically....

             

             

            <?xml version="1.0"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="100%" width="100%" creationComplete="init();">
                <mx:Script>
                    <![CDATA[
                    import mx.controls.Label;
                   
                    private var dataArray:Array = [{data:"1", label:"Label 1"},
                              {data:"2", label:"Label 2"},
                              {data:"3", label:"Label 3"},
                              {data:"4", label:"Label 4"},
                              {data:"5", label:"Label 5"}];

                    private function displayLabels():void {
                     for(var intLoop:int=0;intLoop<dataArray.length;intLoop++)
                     {
                         var lblDisplay:Label = new Label();
                         lblDisplay.text = dataArray[intLoop].label;
                         lblDisplay.setStyle("fontSize", 15);
                         lblDisplay.setStyle("color", 0xCDF3AE);
                         labelBox.addChild(lblDisplay);
                     }
                    }
                    ]]>
                </mx:Script>

                <mx:VBox id="labelBox">
                
                </mx:VBox>
                <mx:Button click="displayLabels()" label="Add Labels Dynamically"/>
            </mx:Application>

             

            Thanks,

            Bhasker