Adding and binding components on the Stage

One of the biggest advantages to using the binding classes with ActionScript is that you can create bindings between components that Flash has added to the Stage at runtime. Imagine creating your own custom class that adds the appropriate text fields to the Stage at runtime, and then validates the necessary data and adds the necessary bindings. As long as you have the components in your library, you can add them dynamically and use a couple of extra lines of code to create bindings.

To add and then bind components on the Stage by using ActionScript:

  1. Create a new Flash document.
  2. Drag a ComboBox and a Label component into the document's library.
  3. Insert a new layer and name it actions.
  4. Add the following code to Frame 1 of the actions layer:
    import mx.data.binding.*;
    this.createClassObject(mx.controls.ComboBox, "my_cb", 1, {_x:10, _y:10});
    this.createClassObject(mx.controls.Label, "my_lbl", 2, {_x:10, _y:40});
    my_cb.addItem("JAN", 0);
    my_cb.addItem("FEB", 1);
    my_cb.addItem("MAR", 2);
    my_cb.addItem("APR", 3);
    my_cb.addItem("MAY", 4);
    my_cb.addItem("JUN", 5);
    var src:EndPoint = new EndPoint();
    src.component = my_cb;
    src.property = "value";
    src.event = "change";
    var dest:EndPoint = new EndPoint();
    dest.component = my_lbl;
    dest.property = "text";
    new Binding(src, dest);
    

    The first line of ActionScript imports the classes from the mx.data.binding package so that you don't need to use fully qualified paths in your code. The next two lines of ActionScript attach the components from the document's library to the Stage. Next you position the components on the Stage.

    Finally you add data to the ComboBox instance and create the binding between the my_cb ComboBox and my_lbl Label component on the Stage.