User Guide

12 Creating Advanced Components
Implementing the layoutChildren() method
The
layoutChildren() method positions subobjects within the confines set by the
UIObject.layoutWidth and UIObject.layoutHeight properties of your component. Each
component should implement this method. Use the
layoutChildren() method rather than the
size() method, which is used primarily by Macromedia Flash designers to perform the same
function.
Use the
UIObject.width and UIObject.height properties of the child controls when changing
the size of the children. These properties are scaled for use inside the component. Use the
layoutWidth and layoutHeight properties when changing the size of the component itself.
These properties are not scaled, because the component is at the top level.
The
layoutChildren() method does not update the screen unless you call an invalidation
method. Flex only calls the
layoutChildren() method if the invalidateLayout() method was
previously called. For more information, see About invalidation” on page 17.
The following example checks for the value of the
labelPlacement property and lays out the
mode_mc object accordingly:
function layoutChildren():Void {
text_mc.setSize(layoutWidth - mode_mc.width, layoutHeight);
if (labelPlacement == "left")
{
mode_mc.move(layoutWidth - mode_mc.width, 0);
text_mc.move(0, 0);
}
else {
mode_mc.move(0, 0);
text_mc.move(mode_mc.width, 0);
}
}
Implementing the draw() method
The
draw() method displays objects on the screen. Whenever the component needs to draw an
interface element, it calls a
draw() method. You use the draw() method to create or modify
elements that are subject to change.
Everything is made visible in the
draw() method. A border does not actually call the drawing API
until its
draw() method is called. Any graphical assets that you bring in for the purposes of
measuring are invisible until the draw() method is called.
Do not call the
draw() method directly. Instead, call one of the invalidation methods, which then
calls the draw() method. Flex also calls the draw() method from the redraw() method.
However, Flex calls the
redraw() method only if the object is invalidated, so you should actually
call an invalidation method if you want Flex to invoke the
draw() or redraw() method. If you
do not call an invalidation method, the component remains invisible unless you set its visibility
property to
true in MXML. For more information, see About invalidation” on page 17.
Flex also calls the
draw() method after the layoutChildren() method.