10 Replies Latest reply on Jun 4, 2010 2:10 AM by BhaskerChari

    how to use addEventListener in custom itemrenderer..?

    Ling's Level 1

      Hi.,

                   iam using flex application button click event to be access in custom itemrenderer how.?

       

      With Regards.,

      Lings

        • 1. Re: how to use addEventListener in custom itemrenderer..?
          BhaskerChari Level 4

          Do u want to capture the event in all the items of itemrenderer when button is clicked...

           

          Can you please tell exactly what is your requirement...???

           

          Thanks,

          Bhasker Chari

          • 2. Re: how to use addEventListener in custom itemrenderer..?
            Ling's Level 1

            hi.,
                   Using application with menubar and button1, button 2. Menubar iconfield is loading
            menubaritemrenderer image. when i click button1 iconfield image visible false  at the same time click button2  image visible true.

             

            any ideas about this.,

             

            Ling's

            • 3. Re: how to use addEventListener in custom itemrenderer..?
              Ling's Level 1

              hi.,
                     Using application with menubar and button1, button 2. Menubar iconfield is loading
              menubaritemrenderer image. when i click button1 iconfield image visible false  at the same time click button2  image visible true.

               

              any idea to about this..

               

               

              Ling's

              • 4. Re: how to use addEventListener in custom itemrenderer..?
                BhaskerChari Level 4

                Hi Lings,

                 

                If you post some sample code that way it would be clear...

                 

                 

                Thanks,

                Bhasker Chari

                • 5. Re: how to use addEventListener in custom itemrenderer..?
                  Ling's Level 1

                  Hi chari.,

                               i'm new in flex., menubaritemrenderer using to  load iconfiled in menubar. image ON button click show icons. and image OFF button click not show icons.. how to solve this coding

                  application.mxmx

                   

                                      <s:Button id="btnon" label="Image ON" click="dispatchEvent(new Event('click',  true));" />
                                    <s:Button id="btoff" label="Image OFF" click="dispatchEvent(new Event('change',  true));" />


                                    <MyMenuBar id="testmenu"   labelField="menuLabel"    menuBarItemRenderer="com.MyMenuBArItem" dataProvider="{Datamenu}"  />


                  MyMenuBArItem.as

                  package com
                  {
                     
                      import flash.events.MouseEvent;
                          import mx.controls.Menu;
                      import mx.controls.MenuBar;
                      import mx.controls.Text;
                      import mx.controls.menuClasses.MenuBarItem;
                      import mx.controls.menuClasses.MenuItemRenderer;
                      import mx.rpc.events.ResultEvent;
                     
                      public class MyMenuBArItem extends  MenuBarItem
                      {
                          private var image:Image;
                          private var imgtest:Image;
                          private var text:Text;
                       
                         
                          public function MyMenuBArItem()
                          {
                              super();
                             
                          }
                         
                         
                          override protected function commitProperties():void
                          {
                              super.commitProperties();
                         
                              var icon:String = data.menuIcon;
                              image = new Image();
                              image.source = icon;
                              addChild(image);
                         
                         parentApplication.addEventListener('change', offchange, false,0,true);

                   

                          parentApplication.addEventListener('click', onchange,false,0,true);
                             
                   
                             
                         
                          }
                         
                         
                          public  function offchange(e:Object):void
                          {
                              if(e.type=="change")
                              {
                                  image= new Image();
                                  image.source="";
                                  image.visible= false;
                              }
                          }
                                 
                         public function onchange(e:Object):void
                          {
                             
                               if(e.type=="click")
                                  {
                                   image= new Image();
                                   image.source="";
                                   image.visible= false;
                                   }

                   

                  }

                   

                         override protected function measure():void

                   

                          {
                              super.measure();
                             
                              if (image)
                              {
                                  measuredWidth += image.measuredWidth +2;
                                  measuredHeight = Math.max(image.measuredHeight, measuredHeight);
                              }
                          }
                         
                          override protected function updateDisplayList(unscaledWidth:Number,
                                                                        unscaledHeight:Number):void
                          {
                              super.updateDisplayList(unscaledWidth, unscaledHeight);
                             
                              if (image)
                              {
                                  image.x = (20 - image.measuredWidth) / 2;
                                  image.setActualSize(image.measuredWidth, image.measuredHeight);
                                  label.x = 20;
                              }
                          }
                      }
                  }

                   

                  this way of coding is correct..?

                  or any other solution to solve this

                   

                  Ling's

                  • 6. Re: how to use addEventListener in custom itemrenderer..?
                    BhaskerChari Level 4

                    Hi Lings,

                     

                    Try to dispatch the events as shown below:

                     

                    <s:Button id="btnon" label="Image ON" click="Application.application.systemManager.dispatchEvent(new Event('click',  true));" />
                    <s:Button id="btoff" label="Image OFF" click="Application.application.systemManager.dispatchEvent(new Event('change',  true));" />

                     

                    And In your MenuBarItem.as try to use the following event listeners:

                     

                    systemManager.addEventListener(

                     

                    systemManager.addEventListener('change', offchange, false,0,true);
                    systemManager.addEventListener('click', onchange,false,0,true);

                     

                     

                    Hope this helps you.

                     

                    If this post answers your question or helps, please kindly mark it as such.


                    Thanks,

                    Bhasker Chari

                    • 7. Re: how to use addEventListener in custom itemrenderer..?
                      BhaskerChari Level 4

                      Hi Lings,

                       

                      Did it worked for you...?

                       

                      Thanks.

                      • 8. Re: how to use addEventListener in custom itemrenderer..?
                        Ling's Level 1

                        Hi chari.,

                         

                        Not working., same issues.,

                        override commitproperties() using

                         

                        systemManager.addEventListener('change', offchange, false,0,true);
                        systemManager.addEventListener('click', onchange,false,0,true);

                         

                        everytime override function called that's y image was displayed all time. image on and image off button event occured in MyMenuBArItem.as page

                        but image was displayed.

                         

                        chari.. any ideas to solve this problem..

                         

                        Lings

                        • 9. Re: how to use addEventListener in custom itemrenderer..?
                          BhaskerChari Level 4

                          Hi Lings,

                           

                          Now you the event is being triggered right...earliar it was not working..Is it right..?

                          Now the problem that you have got is making image visible false and true when buttons are clicked.

                          I think the problem is with the line of code image= new Image(); in the two functions offchange and onchange in MyMenuBArItem.as you are using.

                          Since in your commitProperties function you have already instantiated the image object and added image to the stage and you are again trying to instantiate the image object in offchange and onchange events which you shouldn't do. So now image refers to new image instance and does not refer to the object you added in commit properties that's the reason why its not working...

                           

                          So remove the line of code image= new Image();  in both the function(offchange and onchange) and check it I hope it will definetely work out for you...

                           

                          So the new function should become as below:

                           

                          public  function offchange(e:Object):void
                                  {
                                      if(e.type=="change")
                                      {
                                          image.source="";
                                          image.visible= false;
                                      }
                                  }
                                         
                                 public function onchange(e:Object):void
                                  {
                                     
                                       if(e.type=="click")
                                          {

                                           image.source="";
                                           image.visible= false;
                                           }

                           

                          }

                           

                          If this post answers your question or helps, please kindly mark it as such.


                          Thanks,

                          Bhasker Chari

                          • 10. Re: how to use addEventListener in custom itemrenderer..?
                            Ling's Level 1

                            Hi chari.,

                             

                                         Already i changed code., but not working., the reason is when i clicked image OFF button event is triggered on offchange() function,  again call onchange() function also.Both function  event triggered in one button click(ON &OFF). override function of  addeventlistener() executed agin and again.

                             

                            any other way..?

                             

                            public  function offchange(e:Object):void
                                    {
                                        if(e.type=="change")
                                        {
                                          
                                            image.visible= false;
                                        }
                                    }
                                           
                                   public function onchange(e:Object):void
                                    {
                                       
                                         if(e.type=="click")
                                            {

                                           
                                             image.visible= true;
                                             }

                             

                            }

                             

                            With Regards.,

                            Lings