Friday, December 30, 2011

Flex 4 : Dynamic Add Images

Dynamic create image using AS3.0 script, each created image will add in the event listener handler.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx"
      viewSourceURL="srcview/index.html">
 <fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
 </fx:Declarations>
 <fx:Script>
  <![CDATA[
   
   import mx.controls.Alert;
   import flash.display.Sprite;
   import mx.controls.Image;
   
   private function handleImageClick(event:Event):void
   {
      // your click handler code
      Alert.show("Click on me!! ID ="+ event.currentTarget.id);
   }
   
   private function createTile():void
   {
      
      var image:Image = new Image();
      image.id = "img_"+tileBox.numChildren;
      image.width = 50;
      image.height = 50;
      image.source = "http://2.bp.blogspot.com/-Z87UUadFXlo/TkDokmzJTDI/AAAAAAAAB20/4ZqPpP8nwdI/s1600/flex.png";
      image.addEventListener(MouseEvent.CLICK, handleImageClick);
      tileBox.addElement(image);

   }
    
   protected function BT_add_clickHandler(event:MouseEvent):void
   {
      // TODO Auto-generated method stub
      createTile();
   }
   
  ]]>
 </fx:Script>
 <s:TileGroup id="tileBox" left="50" right="50" top="50" bottom="50"
     contentBackgroundColor="#D3B4B4" horizontalGap="2" orientation="rows"
     verticalGap="2">
 </s:TileGroup>
 <s:Button id="BT_add" left="50" top="21" label="Add Image" click="BT_add_clickHandler(event)"/>
 <s:Label left="152" top="29" text="Click on image get ID"/>
</s:Application>

No comments:

Post a Comment

Your comments is appreciate.