Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 18: Using Styles and Themes > Using the setStyle() and getStyle() metho... - Pg. 617

ADOBE FLEX 3 627 Adobe Flex 3 Developer Guide Using the setStyle() and getStyle() methods You cannot get or set style properties directly on a component as you can with other properties. Instead, you set style properties at run time by using the getStyle() and setStyle() ActionScript methods. When you use the getStyle() and setStyle() methods, you can access the style properties of instances of objects or of style sheets. Every Flex component exposes these methods. When you are instantiating an object and setting the styles for the first time, you should try to apply style sheets rather than use the setStyle() method because it is computa- tionally expensive. This method should be used only when you are changing an object's styles during run time. For more information, see "Improving performance with the setStyle() method" on page 631. Setting styles The getStyle() method has the following signature: var:return_type componentInstance.getStyle(property_name) The return_type depends on the style that you access. Styles can be of type String, Number, Boolean, or, in the case of skins, Class. The property_name is a String that indicates the name of the style property--for example, fontSize . The setStyle() method has the following signature: componentInstance.setStyle(property_name, property_value) The property_value sets the new value of the specified property. To determine valid values for properties, see the Adobe Flex Language Reference. The following example uses the getStyle() and setStyle() methods to change the Button's fontSize style and display the new size in the TextInput: <?xml version="1.0"?> <!-- styles/SetSizeGetSize.mxml --> <mx:Application xmlns:mx="" creationComplete="initApp()"> <mx:Script><![CDATA[ [Bindable] private var curSize:int = 10; private function initApp():void { ip1.setStyle("fontSize", curSize); b1.setStyle("fontSize", curSize); b2.setStyle("fontSize", curSize); } public function showStyles():void {"Font size is " + ip1.getStyle("fontSize") + "."); }