User Guide
946 Chapter 7: Creating Components
The init() method is called from UIObject’s constructor, so the flow of control climbs up the
chain of constructors until it reaches UIObject. UIObject’s constructor calls the
init() method
that is defined on lowest subclass. Each implementation of
init() should call super.init() so
that its base class can finish initializing. If you implement an
init() method and you don’t call
super.init(), the ()init method is not called on any of the base classes, so they might never be
in a usable state.
Defining the createChildren() method
Components implement the
createChildren() method to create subobjects (such as other
components) in the component. Rather than calling the subobject’s constructor in the
createChildren() method, call createClassObject() or createObject() to instantiate a
subobject of your component.
It’s a good idea to call
size() within the createChildren() method to make sure all children
are set to the correct size initially. Also, call
invalidate() within the createChildren()
method to refresh the screen. (For more information, see “About invalidation” on page 948.)
The
createClassObject() method has the following syntax:
createClassObject(className, instanceName, depth, initObject)
The following table describes the parameters:
To call
createClassObject(), you must know what the children are, because you must specify
the name and type of the object, plus any initialization parameters in the call to
createClassObject().
The following example calls
createClassObject() to create a new Button object for use inside a
component:
up_mc.createClassObject(mx.controls.Button, "submit_btn", 1);
You set properties in the call to createClassObject() by adding them as part of the
initObject parameter. The following example sets the value of the label property:
form.createClassObject(mx.controls.CheckBox, "cb", 0, {label:"Check this"});
The following example creates TextInput and SimpleButton components:
function createChildren():Void {
if (text_mc == undefined)
createClassObject(TextInput, "text_mc", 0, { preferredWidth: 80,
editable:false });
text_mc.addEventListener("change", this);
text_mc.addEventListener("focusOut", this);
Parameter Type Description
className
Object The name of the class.
instanceName
String The name of the instance.
depth
Number The depth for the instance.
initObject
Object An object that contains initialization properties.