User Guide
Table Of Contents
- Contents
- Working with Flash MX 2004
- Creating Basic Components in Flash MX 2004
- Creating Advanced Components in Flash MX 2004
- Contents
- About Creating components
- Writing the component’s ActionScript code
- Simple example of a class file
- General process for writing a class file
- Selecting a parent class
- Identifying the class, symbol, and owner names
- About the component instantiation life cycle
- Writing the constructor
- Specifying clip parameters
- Implementing the constructObject2() method
- Implementing the init() method
- Implementing the createChildren() method
- Implementing the commitProperties() method
- Implementing the measure() method
- Implementing the layoutChildren() method
- Implementing the draw() method
- Defining getters and setters
- Component metadata
- Defining component parameters
- Handling events
- Using the Event metadata
- About invalidation
- Skinning custom controls
- Adding styles
- Making components accessible
- Improving component usability
- Best practices when designing a component
- Using the ModalText example
- Troubleshooting
- Index

Writing the component’s ActionScript code 57
By default, Flex sets the values of _measuredPreferredWidth and _measuredPreferredHeight
to the values of the current height and width, but you should override them. The following
example of the
measure() method from the Label component sets a default width and height if
the label text field is empty:
function measure(Void):Void {
var myTF = _getTextFormat();
var txt = text;
if (txt == undefined || txt.length < 2) {
txt = "Wj";
}
var textExt = myTF.getTextExtent2(txt);
var textW = textExt.width + 4;
var textH = textExt.height + 4;
if (textW == undefined) {
textW = 20;
}
if (textH == undefined) {
textH = 8;
}
trace("Label: " + textW + " " + textH);
_measuredPreferredWidth = textW;
_measuredPreferredHeight = textH;
}
Implementing the layoutChildren() method
The
layoutChildren() method positions subobjects within the confines set by the
layoutWidth and layoutHeight properties of your component. Each component should
implement this method. The
layoutChildren() method deprecates the size() method, which
is used primarily by Flash designers to perform the same function.
Use the width and 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 68.
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")