background image

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.

Summary of Contents for FLEX-CREATING ADVANCED COMPONENTS

Page 1: ...Creating Advanced Components...

Page 2: ...y website mentioned in this guide then you do so at your own risk Macromedia provides these links only as a convenience and the inclusion of the link does not imply that Macromedia endorses or accepts...

Page 3: ...ateChildren method 9 Implementing the commitProperties method 10 Implementing the measure method 10 Implementing the layoutChildren method 12 Implementing the draw method 12 Defining getters and sette...

Page 4: ...4 Contents...

Page 5: ...About creating components Most new controls extend an existing class If you want to create a component that is based on the Button control for example you can subclass the mx controls Button class How...

Page 6: ...d defines events and event handlers for the component When you extend an existing component class you can inherit from only one class ActionScript 2 0 does not allow multiple inheritance To edit Actio...

Page 7: ...lasses broadcast their events just before drawing If you are familiar with Flash this event is analogous to the enterFrame MovieClip event The UIObject class posts events to its listeners just before...

Page 8: ...ify a Button s behavior it is simpler to extend the Button class as a custom MXML component For more information see Developing Flex Applications About the component instantiation life cycle When you...

Page 9: ...ct method to instantiate a subobject of your component The createClassObject method has the following signature createClassObject className instanceName depth initObject The following table describes...

Page 10: ...e ViewStack selectedIndex property the ViewStack container does not display a new page right away Instead it privately stores a pendingSelectedIndex property When it is time for Flash Player to update...

Page 11: ...s how wide its label is in order to compute the value of the _measuredPreferredWidth property Note Although you can let Flex determine these values for you your application startup time will decrease...

Page 12: ...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 t...

Page 13: ...edia recommends that you use initial capital letters for the second word and each word that follows For example public function get initialColorStyle Number The variable that stores the property s val...

Page 14: ...geProp1 when the property is modified ChangeEvent changeProp1 var prop1 Number You can also instruct your component to generate an event when a getter or setter method is called as the following examp...

Page 15: ...the architecture mx core UIObject and mx core UIComponent These classes define low level component events such as draw resize move load and others that are fundamental to all components Subclasses of...

Page 16: ...a subobject you use the createClassObject method as the following example shows function createChildren Void createClassObject TextInput text_mc 0 preferredWidth 80 editable false text_mc addEventLis...

Page 17: ...alls the commitProperties method You should call the invalidateSize method infrequently because it measures and redraws everything on the screen and this can be a computationally expensive action Some...

Page 18: ...lity When developers add the MyButton component to an application they can use the Accessibility panel to make it available to screen readers Adding versioning When releasing components you should def...

Page 19: ...xt example The following code example implements the class definition for the ModalText component The ModalText component is a text input whose default is the noneditable mode but you can switch to ed...

Page 20: ...SkinName falseDownSkin modeDownSkinName mode_mc addEventListener click this e Implement the measure method The default width is the size of the text plus the button The height is dictated by the butto...

Page 21: ...tchEvent type placementChanged function get labelPlacement String return __labelPlacement private var __text String ModalText ChangeEvent textChanged Inspectable defaultValue ModalText function set te...

Page 22: ...ML Ensure that the attribute is spelled correctly Also be sure that it is not private I don t get any errors but nothing shows up Verify that the component was instantiated One way to do this is to pu...

Page 23: ...ck the values of the preferredWidth and preferredHeight properties Other code might have set those values to zero If so set a breakpoint in the setters for the width height preferredWidth and preferre...

Page 24: ...24 Creating Advanced Components...

Reviews: