Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-12-2010, 04:36 PM   #1
desmo953
Registered User
 
Join Date: Jun 2008
Posts: 34
Default Get width and height of dynamic components

Trying to get the width of a dynamically created component in flex.

ActionScript Code:
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!
desmo953 is offline   Reply With Quote
Old 05-12-2010, 05:50 PM   #2
NoobsArePeople2
Gamefun Engineer
 
Join Date: Nov 2009
Posts: 233
Default

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:

ActionScript Code:
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.
NoobsArePeople2 is offline   Reply With Quote
Old 05-12-2010, 08:50 PM   #3
desmo953
Registered User
 
Join Date: Jun 2008
Posts: 34
Default

Quote:
Originally Posted by NoobsArePeople2 View Post
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:

ActionScript Code:
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.
desmo953 is offline   Reply With Quote
Old 05-12-2010, 10:17 PM   #4
NoobsArePeople2
Gamefun Engineer
 
Join Date: Nov 2009
Posts: 233
Default

Interesting. What's the stack trace on the error?
NoobsArePeople2 is offline   Reply With Quote
Old 05-13-2010, 01:25 PM   #5
desmo953
Registered User
 
Join Date: Jun 2008
Posts: 34
Default

ActionScript Code:
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()
desmo953 is offline   Reply With Quote
Old 05-14-2010, 03:57 PM   #6
Peter Cowling
quantum tunneler
 
Join Date: Sep 2008
Location: London, UK
Posts: 882
Default

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...
Peter Cowling is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:21 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.