3 Replies Latest reply on Jul 19, 2010 3:05 AM by uzairseedat

    Confusion on RadioButtonGroup numRadioButtons == 0, as I add them dynamically?

    rickcr

      This has me confused. I have a component that I use to generate radiobuttons and a group is added to each button as they are being built. ( After they are all built another process is supposed to iterate over the radiobuttongs in the group and mark a certain one as selected.)

       

      What I'm finding difficult understanding is why my radioButtonGroup.numRadioButtons is 0 even after I assign the radioButtonGroup to the created radiobButton. In fact I can't even get access to a radioButton in the group by getRadioButtonAt(index.)

       

      It looks it's only after everything is rendered that you have access to the radioButtons in the radioButtonGroup? If so, that's somewhat challenging... since I was hoping I could get access to the radio buttons by a reference to the RadioButtonGroup.

       

      As an example here is some code illustrating...

       

      public function build():void {
          radioButtonsBox = (layout == BaseComponentBuilder.VERTICAL) ?new VBox(): new HBox();
          this.rbGroup = new RadioButtonGroup();
          for each(var obj:* in items) {
              var r:RadioButton = new RadioButton();
              r.group = rbGroup;
              r.value = obj.@data ? obj.@data:obj.value;
              r.label = obj.@label ? obj.@label:obj.label;
              trace("rbGroup numRadioButtons afer i just added one! "+ rbGroup.numRadioButtons );//ALWAYS 0!
              trace("rbGroup rb at 0 = "+ rbGroup.getRadioButtonAt(0) );//ALWAYS null
              radioButtonsBox.addChild(r);
          }
          parent.addChild(radioButtonsBox);
          trace("***after build() group num radio buttons = "+rbGroup.numRadioButtons); //STILL 0
      }
        • 1. Re: Confusion on RadioButtonGroup numRadioButtons == 0, as I add them dynamically?
          archemedia Level 4

          Maybe this helps

           

           

          This code works:

           

          <?xml version="1.0" encoding="utf-8"?>

           

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">

           

              <mx:Script>

           

                  <![CDATA[

           

                      import mx.controls.RadioButton;

           

                      import mx.controls.RadioButtonGroup;

           

                      import mx.containers.VBox;

           

           

           

                      private var rbGroup:RadioButtonGroup;

           

                     

           

                      private function init():void

           

                      {

           

                          build();

           

                      }

           

                     

           

                      public function build():void {

           

                          var items:Array = [{value: "one", label: "labelOne"}, {value: "two", label: "labelTwo"}];

           

                         

           

                          var radioButtonBox:VBox = new VBox();

           

                          rbGroup = new RadioButtonGroup();

           

                          for each(var obj:* in items) {

           

                              var r:RadioButton = new RadioButton();

           

                              r.group = rbGroup;

           

                              r.value = obj.value;

           

                              r.label = obj.label;

           

                              radioButtonBox.addChild(r);

           

                          }

           

                          addChild(radioButtonBox);

           

                      }

           

           

                  ]]>

           

              </mx:Script>

           

          </mx:Application>

           

          The radioButtonGroup component is NOT a visual component so it behaves diffrently. This is probably why you don't have access immediately

           

           

          Dany

          • 2. Re: Confusion on RadioButtonGroup numRadioButtons == 0, as I add them dynamically?
            rickcr Level 1

            archemedia wrote:

            The radioButtonGroup component is NOT a visual component so it behaves diffrently. This is probably why you don't have access immediately

             

             

             

            I figured that, but it would be cool if the API made a mention of that on the 'numRadioButtons' and 'getRadioButtonAt' functions, since it's diffently a bit puzzling at first when you try:

             

            var group:RadioButonGroup = new RadioButtonGroup();

            var rb:RadioButton = new RadioButton();

            rb.group = group;

            trace("rbGroupSize = "+group.numRadioButtons); //surprise not 1, but 0!

             

            I'll mark this question as answered if the  answer is:

             

            "The RadioButtonGroup does not have access to the RadioButtons until AFTER the radio buttons are rendered."

            • 3. Re: Confusion on RadioButtonGroup numRadioButtons == 0, as I add them dynamically?
              uzairseedat Level 1

              The numRadioButtons property in RadioButtonGroup does not increment unless the radio buttons are validated. If you wish immediate access to radio buttons through the group, first call validateNow( ) on each radio button.