0
votes

I am writing a class that extends mx.core.UIComponent. In the constructor of this class, I would like to add a Button (spark.components.Button). However, creating a new Button object (such as var b:Button = new Button();) and then doing this.addChild(b) doesn't work -- no button is appearing. Is there any reason why a Button object can't be added as a child?

Edit: Just to add, when I print out the children, it says that the Button object is a child, but the Button doesn't show up.

3

3 Answers

1
votes

Using UIComponent directly, you aren't getting the layout of the child handled for you; you would need to set the position and size of your child components manually (see docs).

Consider using a Group (or Canvas, if you're still on MX components) to handle child layout. You can still add rendering in updateDisplayList, just make sure you call super.updateDisplayList() as well.

2
votes

You need to add the button in the createChildren method not in the constructor.

protected var button:Button;

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

    button = new Button();

    addChild(button);
}

Depending on what you are trying to do you may also need to implement the measure and updateDisplayList methods.

0
votes

You need to read up on the differences between ActionScript components and Flex components. When using a Flex component, you must use container.addElement( element ), not container.addChild( child ). As far as I am aware, the only classes with this functionality descend from Group (at least in the Spark framework, the one I am most familiar with). So extending UIComponent will not allow you to add a Button, although a UIComponent can be added to a Flex container (which I do not believe a normal Sprite can do)