PDA

View Full Version : Get width and height of dynamic components


desmo953
05-12-2010, 04:36 PM
Trying to get the width of a dynamically created component in flex.


var check:CheckBox = new CheckBox();
check.label = "Video 1";
trace(check.width); //Returning 0


It keeps returning the checkbox's width as 0 even tho now it has content and the width should reflect that. Any ideas how to get flex to measure the new width?

Any help will be appreciated!

NoobsArePeople2
05-12-2010, 05:50 PM
Read up on the Flex component life cycle, specifically the invalidation methods.

Basically what's happening is that you are asking for the width immediately after changing the text but Flex doesn't update the width until the next frame.

There are two ways you can work with this:

1) callLater()
2) invalidation methods

callLater will execute the provided function on a later frame, allowing Flex time to update the dimensions of your CheckBox.

Using the invalidation methods will force Flex to do all the updating immediately. In your case I believe you'd want to do something like:



var check:CheckBox = new CheckBox();
check.label = "Video 1";
check.invalidateSize();
check.validateSize();
trace(check.width);



My personal feeling is that using the invalidation methods in this manner is working against Flex rather than with it. You'd probably be better served by overriding updateDisplayList() and doing your sizing there.

desmo953
05-12-2010, 08:50 PM
Read up on the Flex component life cycle, specifically the invalidation methods.

Basically what's happening is that you are asking for the width immediately after changing the text but Flex doesn't update the width until the next frame.

There are two ways you can work with this:

1) callLater()
2) invalidation methods

callLater will execute the provided function on a later frame, allowing Flex time to update the dimensions of your CheckBox.

Using the invalidation methods will force Flex to do all the updating immediately. In your case I believe you'd want to do something like:



var check:CheckBox = new CheckBox();
check.label = "Video 1";
check.invalidateSize();
check.validateSize();
trace(check.width);



My personal feeling is that using the invalidation methods in this manner is working against Flex rather than with it. You'd probably be better served by overriding updateDisplayList() and doing your sizing there.


I get this error trying your invalidate/validate method Parameter antiAliasType must be non-null.

NoobsArePeople2
05-12-2010, 10:17 PM
Interesting. What's the stack trace on the error?

desmo953
05-13-2010, 01:25 PM
TypeError: Error #2007: Parameter antiAliasType must be non-null.
at flash.text::TextField/set antiAliasType()
at mx.core::UITextFormat/measure()
at mx.core::UITextFormat/measureText()
at mx.core::UIComponent/measureText()
at mx.controls::Button/measure()
at mx.controls::CheckBox/measure()
at mx.core::UIComponent/measureSizes()
at mx.core::UIComponent/validateSize()
at buildTest/result2()[C:buildTest.mxml:115]


Line 115 is where I call check.validateSize()

Peter Cowling
05-14-2010, 03:57 PM
Add an event listener to your checkbox, and listen for mx.events.ResizeEvent.RESIZE. When that event had been triggered you can be sure the component's size has changed - its not dispatched until it has. Then just grab the actual size...