views:

703

answers:

2

I have a UI component that, for various reasons, I have to construct programatically. The component is a table of radio buttons grouped by column.

Right now, I'm constructing the column groups like so:

private function createGroupsForItemList(items: XMLList): void {
    for each (var item: XML in items) {
        var rbGroup: RadioButtonGroup = new RadioButtonGroup();
        groups[[email protected]()] = rbGroup;
    }
}

I'm trying to associate the RadioButton instances with the column groups like so:

private function createValueControl(item: XML): UIComponent {
    var control: RadioButton = new RadioButton();
    control.label = "";
    control.group = groups[[email protected]()];
    control.addEventListener(Event.SELECT, updateSelection);
    return control;
}

I can see in the debugger that the control has an association to the group:

control.group == groups[[email protected]()]

However, I can see equally that the group does not know anything about the control:

group.radioButtons.length == 0

I imagine that this is because the setter for group in RadioButton is a dumb setter; all it does is copy to the variable, which doesn't do the magic that groupName does. However, I can't seem to find the value I should use to set the RadioButton.groupName property correctly.

So, in short, I'm stumped on how to get these bits to talk to each other. How do I do this?

-- EDIT --

It turns out that I can have the groups created and associated simply by setting the groupName property, but I can't get at the group to set up a selection listener; the group is NULL immediately after the setting process, which means that the second line below throws the Flex equivalent of an NPE:

control.groupName = groupNameForLevel(item);
control.group.addEventListener(Event.SELECT, updateSelection);
A: 

Setting groupName should work.

All I can suggest is to step through the group() getter of the RadioButton component and see where exactly it is failing. Are you programmatically creating the group too? If that's the case, maybe it isn't initialized fully yet.

Chetan Sastry
+1  A: 

First instinct is that this issue has to do with invalidateDisplayList and when and how that is called. Of course, since issues related to that function are behind a number of Flex's quirks, I may just be scapegoating.

This is not the answer to your question per se, but it seems like it might actually work as an alternate solution.

RadioButtonGroups will initialize based on a IFlexDisplayObject. This means that you can do something like:

var c:HBox               = new HBox();
var rbg:RadioButtonGroup = new RadioButtonGroup( c );
// do stuff with rbg.
c.addChild( new RadioButton() );

The problem is that it may not be the most practical answer, but it has the decided benefit of being a workable solution.

Christopher W. Allen-Poole