15 Replies Latest reply on Aug 3, 2006 12:27 AM by Newsgroup_User

    RadioButtonGroup

    CKLThomco
      I am trying to create RadioButtonGroup and add RadioButtons to it thru ActionScript.

      My code :
      var gbField:RadioButtonGroup = new RadioButtonGroup();
      gbField.id = txtfield.text;

      that last lines results in an error "1119: Access of possibly undefined property id through a reference with static type mx.controls:RadioButton Group.

      WHen I drag/drop a RadioButtonGroup in <mx> it has an "id" attribute.

      What do I need to do in A/S ?

      Thanks,
      Keith Lowry
      Atlanta, GA
        • 1. Re: RadioButtonGroup
          inlineblue Level 1
          I'm not sure why there's a lack of symmetry between MXML and AS for this, but from what I can tell, if you're using AS, you don't assign an id to the group and you don't use the RadioButton's groupName property. Instead, you just create the RadioButtonGroup and then use the RadioButton's group property. eg.

          var radGroup: RadioButtonGroup = new RadioButtonGroup();
          radGroup.addEventListener(...);

          var rbtn: RadioButton = new RadioButton();
          rbtn.group = radGroup;
          //etc...
          • 2. Re: RadioButtonGroup
            CKLThomco Level 1
            I tried that and I get the following error :
            "ReferenceError: Error #1069: Property group not found on mx.controls.RadioButtonGroup and there is no default value"
            • 3. Re: RadioButtonGroup
              inlineblue Level 1
              No, look at my example again. Don't assign any properties on RadioButtonGroup. Assign the "group" property on the RadioButton objects (so that the buttons belong to the group).
              • 4. Re: RadioButtonGroup
                CKLThomco Level 1
                Correct.

                All I did was add the change event listener to the RBG. Everything else is assigned to the RadioButton. But since the RBG is creating the event now instead of the button, event.currentTarget doesn't know about the id, group, groupname, etc of the RadioButton.
                • 5. Re: RadioButtonGroup
                  Flex harUI Adobe Employee
                  You should set the groupName of the RGB. That will distinguish it from other RGBs.

                  IDs are properties of the document containing the RGB and not a property of RGB itself. When you say id="foo" it gets translated into "var foo:RadioButtonGroup".
                  • 6. Re: RadioButtonGroup
                    inlineblue Level 1
                    Well, you can still go ahead and add event listeners to each individual button. My example was just using the group as a central event handler.
                    • 7. Re: RadioButtonGroup
                      CKLThomco Level 1
                      Flex harUI, I tried setting the groupName for the RGB but A/S didnt like it.

                      inlineblue, yes I was able to add the event to each button , but that sadly returns two values (one for each radio button) and what I need it 1 value (the value of the active radio button).
                      • 8. Re: RadioButtonGroup
                        inlineblue Level 1
                        This is what I did, and only one event is fired when a button is clicked:

                        radGroup = new RadioButtonGroup();

                        var rbtn: RadioButton = new RadioButton();
                        rbtn.group = radGroup;
                        rbtn.addEventListener("click", onClick);
                        myvbox.addChild(rbtn);

                        rbtn = new RadioButton();
                        rbtn.group = radGroup;
                        rbtn.addEventListener("click", onClick);
                        myvbox.addChild(rbtn);

                        Are you sure you removed the listener on the RBG?
                        • 9. Re: RadioButtonGroup
                          CKLThomco Level 1
                          That works to an extent. But in my event handler when i try
                          textInp.text = event.currentTarget.group; i get [object RadioButtonGroup] as the value. How do I access the actual name/id/group/groupName of the RBG ? and then access teh actual value of the RB that was selected ?
                          • 10. Re: RadioButtonGroup
                            inlineblue Level 1
                            A RadioButtonGroup does not have a name. Its only purpose is to group RadioButtons and it really should never be referred to again.

                            Now, if you need a simple way to label what group a RadioButton belongs to, you can go ahead and assign the RadioButton's groupName property. When set in AS, the groupName property doesn't do anything--it's just a plain string. So, when creating your buttons, you can do this:

                            var rbtn: RadioButton = new RadioButton();
                            rbtn.group = radGroup; //This sets the group
                            rbtn.groupName = "walalala"; //This doesn't actually do anything
                            rbtn.addEventListener("click", onClick);
                            myvbox.addChild(rbtn);

                            In your click handler, event.currentTarget is the RadioButton that was clicked. So event.currentTarget.groupName would be "walalala" and event.currentTarget.value is the value of the RadioButton.
                            • 11. Re: RadioButtonGroup
                              CKLThomco Level 1
                              I actually had it set up this way a few days ago but I ran in to some odd behavior and abondoned it. I retried it again and still got the odd behavior.

                              If I set it up like that and in the event handler put just a simple command like textNew.text = event.currentTarget.value;
                              When I click the first radio button the value of the first RB is displayed. great :-)
                              When I click the 2nd radio button nothing happens :-(
                              When I click the 1st RB again the value of the 2nd RB is displayed and from that point on the values are reversed showing the value of the opposing RB instead of it's own value.
                              • 12. Re: RadioButtonGroup
                                inlineblue Level 1
                                You'll have to post the code that you're using. It's too difficult to diagnose in general terms.
                                • 13. Re: RadioButtonGroup
                                  Level 7
                                  inlineblue escribi�:
                                  > A RadioButtonGroup does not have a name. Its only purpose is to group
                                  > RadioButtons and it really should never be referred to again.
                                  >
                                  > Now, if you need a simple way to label what group a RadioButton belongs to,
                                  > you can go ahead and assign the RadioButton's groupName property. When
                                  > set in AS, the groupName property doesn't do anything--it's just a plain
                                  > string. So, when creating your buttons, you can do this:
                                  >
                                  > var rbtn: RadioButton = new RadioButton();
                                  > rbtn.group = radGroup; //This sets the group
                                  > rbtn.groupName = "walalala"; //This doesn't actually do anything
                                  > rbtn.addEventListener("click", onClick);
                                  > myvbox.addChild(rbtn);
                                  >
                                  > In your click handler, event.currentTarget is the RadioButton that was
                                  > clicked. So event.currentTarget.groupName would be "walalala" and
                                  > event.currentTarget.value is the value of the RadioButton.
                                  >

                                  inlineblue, i'm new to Actionscript but a senior programmer with
                                  Javascript, so let me show you a piece of AS code where 'id' property
                                  should be public exposed:

                                  The purpose of this piece of code is to prepare an array to send data to
                                  a server.

                                  b contains objects from the mxml and one of them is a RadioButtonGroup.

                                  var a:Array=new Array();
                                  var b:Array=[dni,ape,nom,dirvia,dirdir,sexo];

                                  for(var i:Object in b){

                                  var className:String = getQualifiedClassName(b );
                                  // strip package
                                  var index:int = className.indexOf("::");
                                  if (index != -1)className = className.substr(index + 2);

                                  switch(className){
                                  case 'ComboBox':
                                  a[ b
                                  .id] = b .value;
                                  break;
                                  case 'RadioButtonGroup':
                                  a[ b
                                  .id] = b .selectedValue;
                                  break;
                                  case ...
                                  ...
                                  default:
                                  a[ b
                                  .id]=b .text;
                                  }
                                  }
                                  // Show the a Array
                                  for(var j:String in a){
                                  Alert.show( j+'='+a[j] );
                                  }


                                  In our projects we take Javascript to the limits and now we are trying
                                  to migrate one of them to Flex, so we are going to take AS to the limits
                                  as in Javascript, maybe we still are newbies to Flex and want to run
                                  before learn to walk.

                                  Thanks for your patience.

                                  • 14. RadioButtonGroup
                                    inlineblue Level 1
                                    I'm not too clear what your question is. However, know that a RadioButtonGroup does not have an id property. It inherits from EventDispatcher, not UIComponent. In general, when you do something like this in MXML:

                                    <mx:Object id="xxx">

                                    It doesn't mean create an Object and set its id property to "xxx". What it says is declare a variable of type Object and name that variable "xxx". In the AS code that's generated, you get this:

                                    public var xxx: Object;

                                    As always, the type determines what properties are present. The Object type does not have a property named id. Now, it just so happens that UIComponent declares a property called id, which is why every visual component has an id property that you can use in AS. But RadioButtonGroup is not a UIComponent, so there's no id property.
                                    • 15. Re: RadioButtonGroup
                                      Level 7
                                      Well, after all we're newbies to AS.

                                      Thanks

                                      inlineblue escribi�:
                                      > I'm not too clear what your question is. However, know that a RadioButtonGroup
                                      > goes not have an id property. It inherits from EventDispatcher, not
                                      > UIComponent. In general, when you do something like this in MXML:
                                      >
                                      > <mx:Object id="xxx">
                                      >
                                      > It doesn't mean create an Object and set its id property to "xxx". What it
                                      > says is declare a variable of type Object and name that variable "xxx". In the
                                      > AS code that's generated, you get this:
                                      >
                                      > public var xxx: Object;
                                      >
                                      > As always, the type determines what properties are present. The Object type
                                      > does not have a property named id. Now, it just so happens that UIComponent
                                      > declares a property called id, which is why every visual component has an id
                                      > property that you can use in AS. But RadioButtonGroup is not a UIComponent, so
                                      > there's no id property.
                                      >
                                      >