Link to home
Create AccountLog in
Avatar of joebox
joebox

asked on

How to get actionscript generated component to update its properties

Oh man I am obviously missing something here but not sure what it is.  How can I get the HBox to update its width after an addChild().  I am tracing our the width before and after the addChild but it doesnt change.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	 layout="absolute"
	 creationComplete="init();">
	<mx:Script>
		<![CDATA[
			import mx.controls.Label;
			import mx.containers.HBox;
			
			private function init():void {
				var row:HBox = new HBox();
				var txt:Label = new Label();
				txt.text = "just some text";
				
				trace(row.width.toString());
				row.addChild(txt);
				trace(row.width.toString());
			}	
		]]>
	</mx:Script>
</mx:Application>

Open in new window

Avatar of taus01
taus01
Flag of Germany image

Try calling the one or both of following Methods before you add the child:

/* Marks a component so that its updateDisplayList() method gets called during a later screen update */
row.invalidateDisplayList();
 
/* Marks a component so that its measure() method gets called during a later screen update. */
row.invalidateSize();

Open in new window

Avatar of joebox
joebox

ASKER

thanks for the tip but it didn't bring any luck.  Both traces are still coming up as 0.  Here is the updated code that i ran.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	 layout="absolute"
	 creationComplete="init();">
	<mx:Script>
		<![CDATA[
			import mx.controls.Label;
			import mx.containers.HBox;
			
			private function init():void {
				var row:HBox = new HBox();
				this.addChild(row);
				var txt:Label = new Label();
				txt.text = "just some text";
				
				trace(row.width.toString());
				row.invalidateDisplayList();
				row.invalidateSize();
				row.addChild(txt);
				trace(row.width.toString());
			}	
		]]>
	</mx:Script>
</mx:Application>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gary Benade
Gary Benade
Flag of South Africa image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of joebox

ASKER

Sorry took so long to get back to you.  You rock for helping out.   Your solution does work I just wanted to ask you one more thing about it.  Is there any way to get the length of the string before you add it to the container.

var txt:Label = new Label();
txt.text = "just some text";
txt.validateNow():
trace(txt.measuredWidth);

this doesnt work.
.  
Check out
http://livedocs.adobe.com/labs/air/1/aslr/flash/text/TextLineMetrics.html
http://blog.greensock.com/textmetrics/

From my experience these classes don't bother measuring themselves until they are added to the display list.
Avatar of joebox

ASKER

hey I thank you very very much for all your help.