Archive for December 13th, 2005

Flex Example: createChild and destroyChild

Create three files called CreateDestroyChild.mxml, VBoxPage.mxml and PanelPage.mxml.

CreateDestroyChild.mxml:

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*"
  3.     width="100%" height="100%" initialize="createUI( 'pageVBox' )">
  4.    
  5.     <mx:Script>
  6.         <![CDATA[
  7.             import mx.core.UIObject;
  8.             public var pageItem : Object;
  9.            
  10.             function createUI( pageUI:String ):Void {
  11.                 if( pageUI == "pageVBox" )
  12.                     pageItem = vsPageHolder.createChild( VBoxPage, undefined );   
  13.                 if( pageUI == "pagePanel" )
  14.                     pageItem = vsPageHolder.createChild( PanelPage, undefined );           
  15.             }
  16.  
  17.             public function destroyUI():Void {
  18.                 vsPageHolder.destroyChild( UIObject( pageItem ) );
  19.             }         
  20.         ]]>
  21.     </mx:Script>
  22.     <mx:HBox width="100%" textAlign="center">
  23.         <mx:ComboBox id="cmbPages" change="destroyUI();createUI( cmbPages.selectedItem.data );">
  24.             <mx:dataProvider>
  25.                 <mx:Array>
  26.                     <mx:Object data="pageVBox" label="Page with VBox"/>
  27.                     <mx:Object data="pagePanel" label="Page with Panel"/>
  28.                 </mx:Array>
  29.             </mx:dataProvider>
  30.         </mx:ComboBox>
  31.         <mx:Button label="Destroy UI" click="destroyUI();" />
  32.         <mx:Button label="Create UI" click="createUI( cmbPages.selectedItem.data );" />  
  33.     </mx:HBox>
  34.     <mx:ViewStack id="vsPageHolder" width="100%" height="100%">
  35.     </mx:ViewStack>
  36. </mx:Application>

VBoxPage.mxml

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:VBox xmlns:mx="http://www.macromedia.com/2003/mxml"
  3.     width="200" height="200"
  4.     backgroundColor="0x996633">
  5.     <mx:Text text="VBox Page, Hello World!"/>
  6. </mx:VBox>

PanelPage.mxml:

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Panel xmlns:mx="http://www.macromedia.com/2003/mxml"
  3.     width="300" height="300"
  4.     backgroundColor="0x336699">
  5.     <mx:Text text="Panel Page, Hello World!"/>
  6. </mx:Panel>

This simple example shows the power of dynamically creating and destroy components at runtime.

1 comment December 13th, 2005



Disclaimer: I work as a Flash/Flex Developer for Adobe Systems Incorporated. The opinions expressed here represent my own and not those of my employer.

My Amazon.com Wish List

Calendar

December 2005
S M T W T F S
« Aug   Feb »
 123
45678910
11121314151617
18192021222324
25262728293031

Posts by Month

Posts by Category


Flex.org - The Directory for Flex