1 Reply Latest reply on Jan 21, 2008 5:44 AM by m_hartnett

    Dynamically created objects

    sulletf
      Hi !

      when dynamically creating a control and then applying styles, I always get 'undefined' from getStyles afterwards.
      This is kind of the same problem as this one (with id) : http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=585&threadid=1330475

      Example below shows how differently getSyles behave between a tag and a dynamic control :

      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">


      <mx:Script><![CDATA[
      import classes.*;
      import components.*;
      import components.renderers.*;
      import events.paramChangeEvent;
      import flash.display.*;
      import flash.events.*;
      import flash.net.*;
      import mx.binding.*;
      import mx.binding.utils.*;
      import mx.collections.ArrayCollection;
      import mx.controls.*;
      import mx.controls.dataGridClasses.*;
      import mx.containers.*;
      import mx.core.*;
      import mx.events.*;
      import mx.managers.*;
      import mx.rpc.events.*;
      import mx.states.*;
      import mx.utils.*;
      import views.*;

      public function fred():void {

      var dg:DataGrid = new DataGrid();
      var dv:String;

      dg.setStyle( 'fontWeight', 'bold' );
      dg.setStyle( 'fontSize', 15 );

      for each ( var str:String in ['fontWeight', 'fontSize'] ) {

      mytextarea1.text+='dg: ' + str + '=' + dg.getStyle( str ) + '\n';
      mytextarea1.text+='mydg: ' + str + '=' + mydg.getStyle( str ) + '\n';
      }


      }
      ]]>

      </mx:Script>

      <mx:Button click="fred()" />
      <mx:TextArea id="mytextarea1" y="500" width="600" height="400"/>
      <mx:DataGrid id="mydg" fontWeight="bold" fontSize="15" />
      </mx:Application>
        • 1. Re: Dynamically created objects
          m_hartnett Level 3
          The problem you are having is that the properties of a dynamically created object are not available until it is added to some container.

          Flex delays calculation of many visual properties until just before it needs them which is only when they are added to a container.

          Here is some modified code to show what is happening. Notice the use of the updateComplete listener to grab the properties as well.

          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          creationComplete="init()">


          <mx:Script><![CDATA[
          import flash.display.*;
          import flash.events.*;
          import flash.net.*;
          import mx.binding.*;
          import mx.binding.utils.*;
          import mx.collections.ArrayCollection;
          import mx.controls.*;
          import mx.controls.dataGridClasses.*;
          import mx.containers.*;
          import mx.core.*;
          import mx.events.*;
          import mx.managers.*;
          import mx.rpc.events.*;
          import mx.states.*;
          import mx.utils.*;

          public function fred():void {

          var dg:DataGrid = new DataGrid();
          var dv:String;


          dg.setStyle( 'fontWeight', 'bold' );
          dg.setStyle( 'fontSize', 15 );
          dg.addEventListener("updateComplete",completeHandler);

          for each ( var str:String in ['fontWeight', 'fontSize'] ) {
          mytextarea1.text+='Loop 1 dg: ' + str + '=' + dg.getStyle( str ) + '\n';
          mytextarea1.text+='Loop 1 mydg: ' + str + '=' + mydg.getStyle( str ) + '\n';
          }

          this.addChild(dg);

          for each ( var str:String in ['fontWeight', 'fontSize'] ) {
          mytextarea1.text+='Loop 2 dg: ' + str + '=' + dg.getStyle( str ) + '\n';
          mytextarea1.text+='Loop 2 mydg: ' + str + '=' + mydg.getStyle( str ) + '\n';
          }



          }

          public function completeHandler(e: Event):void {

          for each ( var str:String in ['fontWeight', 'fontSize'] ) {
          mytextarea1.text+='UpdateComplete dg: ' + str + '=' + e.currentTarget.getStyle( str ) + '\n';
          mytextarea1.text+='UpdateComplete mydg: ' + str + '=' + mydg.getStyle( str ) + '\n';
          }
          }
          ]]>

          </mx:Script>

          <mx:Button click="fred()" />
          <mx:TextArea id="mytextarea1" y="500" width="600" height="400"/>
          <mx:DataGrid id="mydg" fontWeight="bold" fontSize="15" />
          </mx:Application>