crackedcornjimmy
crackedcornjimmy

Reputation: 1982

Dynamically create radio buttons in an HGroup in Flex

I am writing an application in Flex 4.

I created an HGroup like so:

<s:HGroup id="hgMods">
</s:HGroup> 

Then, in Actionscript, I am looping over a collection and adding radio buttons dynamically to a RadioButtonGroup. Then, I want to add that RadioButtonGroup to the HGroup.

Here is my current code:

var rbg:RadioButtonGroup = new RadioButtonGroup();

for each (var obj:[some object] in [some collection]) {             

    var rbGroupName:RadioButton = new RadioButton()
    rbGroupName.label = obj.[some named value].toString()
        rbGroupName.group = rbg;

}

Now, how do I add the RadioButtonGroup to the HGroup?

I tried hgMods.AddChild(rbg);

When I did that, I got the error:

1067: Implicit coercion of a value of type spark.components:RadioButtonGroup to an unrelated type flash.display:DisplayObject.

Upvotes: 0

Views: 1677

Answers (1)

RIAstar
RIAstar

Reputation: 11912

As the error message says: RadioButtonGroup is not a DisplayObject, which means it's not a visual element and hence you can not add it to the display list. RadioButtonGroups function is rather to group RadioButtons together logically, not visually.

The solution would be to add every RadioButton to the HGroup directly. You can keep the rest of your code as it is. Simply add this line inside the loop:

hgMods.addElement(rbGroupName);

Upvotes: 1

Related Questions