tags:

views:

48

answers:

2

hi,

How can I get the width of my LinkButton object ?

myLinkButton = new LinkButton(); myLinkButton.label = "blabla";
myLinkButton.setStyle("fontSize", 24);

myContainer.addChild(myLinkButton); trace (myContainer.width); //this doesn't work because I haven't directly set the attribute

thanks

+1  A: 

First, what does that trace() show? Is it null or undefined or NaN or simply a wrong value?

Then, there are several ways I can think of how you could get around this problem:

  • Try using getBounds() or getRect(). These methods return a Rectangle object working as the DisplayObject's bounding box (including all coordinates and dimensions). Sometimes Flex behaves a bit weird and returns wrong/off results for the coordinates or dimensions of objects.

  • Try experimenting with validateSize() and/or measuredWidth. Perhaps you're trying to access the width property too soon so that Flex cannot do the measuring/layouting in time.

  • Similar idea: what happens if you use myContainer.callLater(trace, [myContainer.width]); (assuming your myContainer inherits from UIComponent)? If you do get a valid result using callLater() but not when accessing width directly then Flex just hasn't had a chance to layout and update the container.

  • You could also try using this method, which creates a Bitmap from the object and returns the Bitmap's height/width. This is especially useful if you have components with visible = false in your container, because Flex doesn't handle invisible components well in that regard.

  • Finally, you could try accessing $width in the mx_internal namespace and check that property's value. However, using mx_internal is sort of a very ugly hack because these properties and methods weren't meant for external use and are subject to change any time (so your component could stop working when a new version is released) - so use with caution.

Baelnorn
A: 

Thanks a lot Baelnorn, you helped me a lot. I used linkButton.validateSize() before printing out width and it worked.

inmatevip