views:

166

answers:

2

I have a label component in a mxml file like below

<mx:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/>

if i want to access it and change its text content within a method defined in action script class that i have written, how to do it?

lble.text="test";
+1  A: 

The ID attribute makes it a private variable within the class or component, so

lble.text = "test";

is just fine.

You are talking about doing this within the same component or class, right? If not you should bind the value to a variable and use getters and setters, like so

[Bindable]
private var _labelText:String;

public function get labelText() : String {
  return _labelText;
}

public function set labelText(value:String) : void {
  _labelText = value;
}

and then

<mx:Label text="{_labelText}"/>
Robusto
but it gives error "access of undefined property lble"
buddhi
I used "_labelText" as the private variable name just for illustration purposes. Change it to whatever you want, but follow the pattern above and you should be fine.
Robusto
+2  A: 

To access the label, you have to import the Label component before the class definition, so it can be accessed:

import mx.controls.Label;

Then, declare the reference to the label in your class body:

public var lble:Label;

And, finally, you can address the label to manipulate it:

lble.text = "Hello world!";
Prutswonder