5 Replies Latest reply on Jun 1, 2011 12:52 AM by naval joshi

    not getting the object refrence from event listner

    naval joshi

      public function category(e:MouseEvent):void
      {  
      trace((IVisualElement(e.currentTarget)));
      //here i want to get button.label.text...
      //but i m not geeting the button reference..


      }


      Code:

      i have no idea how to reach the clicked button properties from the listner...

      i have a simple as3 component say button and a label on it...
      i create 5 buttons from the mxml file looping the objects...now wat i want is when i click on any button i should get its label back..the label is uniqe...

        • 1. Re: not getting the object refrence from event listner
          Claudiu Ursica Level 4

          Can you paste your code?

          C

          • 2. Re: not getting the object refrence from event listner
            naval joshi Level 1

            package Components
            {
                 import flash.events.Event;
                 import flash.events.MouseEvent;
                 import flash.net.FileReference;
                 
                 import mx.controls.Alert;
                 import mx.controls.Button;
                 
                 import spark.components.BorderContainer;
                 import spark.components.Button;
                 import spark.components.Image;
                 import spark.components.Label;
                 import spark.components.Panel;
                 import spark.components.TextInput;
            
                 public class Category extends BorderContainer
                 {
                      public function Category()
                      {
                           super();
                           height=50;
                           width=220;
                           //code for image in the category component
                      
                           i.height=40;
                           i.width=100;
                           
                           i.x=0;
                           i.y=0;
                           
                           
                           addElement(i);
                           
                           //code for labe in category component
                           l.height=45;
                           l.width=130;
                           l.x=100;
                           l.y=10;
                      
                           
                      
                           
                           addElement(l);
                      
                           
                           
                           
                           
                      }
                 public static  var sta:Number=0;
                 public var i:Image= new Image;
            
                 public var l:Label= new Label;
                 
                 }
            }
            this is my component which i m calling
            
            
            in the mxml file im doing
            
            
                 public var cb:Category=new Category;
                    cb.addEventListener(MouseEvent.CLICK,category);
            
             public function category(e:MouseEvent):void
                           {                  
                                
                                //trace(AS3.e.cb.l.text);
                                //trace((IVisualElement(IVisualElement(e.currentTarget)));
                                     //trace(((IVisualElement(e.currentTarget)).AS3).cb.);
                                     //flag=0;
                                                    
                                
                           }
            
            
            i am trying to get the label property of the componet...

            • 3. Re: not getting the object refrence from event listner
              Claudiu Ursica Level 4

              I run your code as follows

               

              package

              {

                  import spark.components.BorderContainer;

                  import spark.components.Image;

                  import spark.components.Label;

                 

                  public class Category extends BorderContainer

                  {

                      public static var sta:Number = 0;

                      public var i:Image;

                     

                      public var l:Label;

                     

                      public function Category()

                      {

                          super();

                          width = 220;

                          height = 50;

                      }

                     

                      override protected function createChildren():void

                      {

                          super.createChildren();

                         

                          i = new Image();

                          i.height=40;

                          i.width=100;

                          addElement(i);

                         

                          l = new Label();

                          l.height=45;

                          l.width=130;

                          l.x=100;

                          l.y=10;

                          addElement(l);

                      }

                  }

              }

               

              Main app

               

              <?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"

                  minWidth="955"

                  minHeight="600"

                  creationComplete="application1_creationCompleteHandler(event)">

                 

                  <fx:Declarations>

                      <!-- Place non-visual elements (e.g., services, value objects) here -->

                  </fx:Declarations>

                 

                  <fx:Script>

                      <![CDATA[

                          import mx.events.FlexEvent;

                         

                          public var cb:Category = new Category();

                         

                          protected function application1_creationCompleteHandler(event:FlexEvent):void

                          {

                              addElement(cb);

                              cb.addEventListener(MouseEvent.CLICK, category);

                          }

                         

                          public function category(e:MouseEvent):void

                          {                 

                             

                              //trace(AS3.e.cb.l.text);

                              //trace((IVisualElement(IVisualElement(e.currentTarget)));

                              //trace(((IVisualElement(e.currentTarget)).AS3).cb.);

                              //flag=0;

                             

                             

                          }

               

                      ]]>

                  </fx:Script>

              </s:Application>

               

               

              A breakpoint in the category handler shows the category as current target.

               

              Are you adding cb on the display list in your code?

               

              C

              • 4. Re: not getting the object refrence from event listner
                naval joshi Level 1

                yes cb is added to the stage...n when clicked i need to retrive its label......

                basically wat i m trying is to get all the properties of the object the event has been fired..

                • 5. found the solution...thanks for replying!
                  naval joshi Level 1

                  public function handleCategoryClicked(event:MouseEvent):void
                  {
                  var clickedButton:Button = event.currentTarget as Button;
                  trace(clickedButton.label);
                  }