6 Replies Latest reply on Aug 10, 2010 1:34 PM by ccay

    Override CreateChildren in ToggleButtonBar?

    ccay

      Hello again,

       

      Now I am working on a custom ToggleButtonBar that I want to put my custom buttons into.

       

      How do I override the create children properly so that the regular buttons are not created and so that I can place my buttons inside instead?

       

      So far I have created a function that loops through the dataprovider and makes my custom buttons:


              private function createAnswerButtons():void{
                  if(!answerImageButton){
                 
                      var i:int;
                      for (i = 0; i < dataProvider.source.length; i++)
                      {
                          answerImageButton = new StepAnswerImageButton();


                          var answerText:String = dataProvider.source[i].answerText;
                          var iconObject:Class = dataProvider.source[i].iconObject;
                         
                          answerImageButton.answerText = answerText;
                          answerImageButton.iconObject = iconObject;


                          this.addChild(answerImageButton);
                      }
                  }

       

      But I am having trouble adding the buttons to the ToggleButtonBar. I have tried different combinations of this:


              override protected function createChildren():void
              {
                  super.removeAllChildren();
                   createAnswerButtons();
                  super.createChildren();
              }

       

      But nothing seems to work.

        • 1. Re: Override CreateChildren in ToggleButtonBar?
          levancho Level 3

          are you getting compilation errors? when you try to override createChildren?


          • 2. Re: Override CreateChildren in ToggleButtonBar?
            ccay Level 1

            Does anyone have any tips to at least point me in the right direction since I have not yet solved this?

            • 3. Re: Override CreateChildren in ToggleButtonBar?
              ccay Level 1

              No, no compilation errors. The application loads, but when I get to the place (a state) where the control loads everything kind of freezes and nothing happens.

              • 4. Re: Override CreateChildren in ToggleButtonBar?
                ShardulSingh Level 3

                Hi,

                 

                You can acheive this like below.

                 

                 

                CustomToggleBar.as

                 

                package
                {
                    import mx.controls.Button;
                    import mx.controls.ToggleButtonBar;
                    import mx.events.FlexEvent;
                   
                    public class CustomToggleBar extends ToggleButtonBar
                    {
                        public function CustomToggleBar()
                        {
                            super();
                            this.addEventListener(FlexEvent.CREATION_COMPLETE,onCreationComplete);
                        }
                       
                        private function onCreationComplete(event : FlexEvent):void
                        {
                            this.removeAllChildren();
                            this.invalidateDisplayList();
                            createCustomButtons();
                        }
                       
                       
                        private function createCustomButtons():void
                        {
                            var btn : Button = new Button();
                            btn.width = 100;
                            btn.height = 50;
                            btn.setStyle('borderColor',0xFF0000);
                            btn.setStyle('borderStyle','solid');
                            btn.label = "Custom Btn";
                            this.addChild(btn);
                        }
                   
                    }
                }

                 

                 

                 

                 

                MainApplication.mxml

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                        layout="vertical"
                        xmlns:local="*">
                  
                    <mx:Script>
                        <![CDATA[
                            import mx.collections.ArrayCollection;
                          
                           [Bindable]
                           private var ac : ArrayCollection = new ArrayCollection([
                           {lbl : "Open"},
                           {lbl : "Delete"},
                           {lbl : "Close"}
                           ])
                          
                        ]]>
                    </mx:Script>
                  
                    <local:CustomToggleBar width="300" height="50" dataProvider="{ac}" labelField="lbl"/>
                  
                </mx:Application>

                 

                 

                 

                 

                Hope this will help you.Let me know if you have any issue.

                 

                 

                 

                 

                 

                 

                 

                 

                with Regards,

                 

                 

                Shardul Singh Bartwal

                • 5. Re: Override CreateChildren in ToggleButtonBar?
                  ccay Level 1

                  Thank you so much for your answer!

                   

                  Something is definitely happening - it does not freeze up as before, but on the other hand it does not seem that my custom buttons are turning up at all.

                   

                  I will keep fiddling around with it and see if I can solve it.

                  But I was wondering what "invalidateDisplayList" does?

                  • 6. Re: Override CreateChildren in ToggleButtonBar?
                    ccay Level 1

                    Ok, I got it to work now.

                    What I had to do for some reason was to set explicit width and height values on the ToggleButtonBar for it to show up at all.

                    (If anyone knows why this is I'd be very interested in hearing about it.)

                     

                    Thanks again!