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
First, what does that
trace()show? Is itnullorundefinedorNaNor simply a wrong value?Then, there are several ways I can think of how you could get around this problem:
Try using
getBounds()orgetRect(). These methods return aRectangleobject working as theDisplayObject‘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/ormeasuredWidth. Perhaps you’re trying to access thewidthproperty 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 yourmyContainerinherits fromUIComponent)? If you do get a valid result usingcallLater()but not when accessingwidthdirectly 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 withvisible = falsein your container, because Flex doesn’t handle invisible components well in that regard.Finally, you could try accessing
$widthin themx_internalnamespace and check that property’s value. However, usingmx_internalis 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.