Macromedia Flex Macromedia Flex
Communicate with App loaded by SWFLoader

Jul 06, 2007 - Communicate with App loaded by SWFLoader
Shows event handlers necessary to know when loaded app can be accessed.

SWFLoader can load a full application inside another.  On issue with an app so loaded is how do you communicate with it.  How do you get access its public properties and methods?  And when is that app ready to be accessed?  It is a bit more complicated than you might think, but here is a working example.


/** SWFLoaded.mxml */
<?xml version="1.0"?>
<mx:Application xmlns:mx=""
  [Bindable]public var varOne:String = "This is a public variable";

  public function setVarOne(newText:String):void {
  <mx:Label id="lblOne" text="I am here"/>
  <mx:Label text="{varOne}"/>
  <mx:Button label="Nested Button"
      click="setVarOne('Nested button pressed.');"/>

/** SWFLoaderTest.mxml */
<?xml version="1.0"?>
<mx:Application xmlns:mx="">

  import mx.controls.Alert;
  import mx.managers.SystemManager;
  import mx.controls.Label;
  import mx.core.Application;
  [Bindable]public var _appLoaded:Application;
  /** run by SWFLoader.complete event */
  private function onCompleteAppLoader(oEvent:Event):void
    var smAppLoaded:SystemManager = SystemManager(;  //get a ref to the loaded app
    //listen for the application.complete event
    smAppLoaded.addEventListener(FlexEvent.APPLICATION_COMPLETE, onCurrentApplicationComplete);
  /** run by application.complete event */
  private function onCurrentApplicationComplete(oEvent:Event):void
    _appLoaded = Application(;
    var sUrl:String = _appLoaded.url;
    SWFLoaded(_appLoaded).setVarOne("This value set from main app!"); //without cast, strict check compiler complains
    lbl.text = "This value from loaded app:" + _appLoaded.url;
  /** Update the Label control in the outer application
   * from the Label control in the loaded application. */
  public function updateLabel():void {
  /** Write to the Label control in the loaded application.*/
  public function updateNestedLabels():void {
      _appLoaded["lblOne"].text = "I was just updated";
      _appLoaded["varOne"] = "I was just updated";
  /** Write to the varOne variable in the loaded application
  * using the setVarOne() method of the loaded application.*/
  public function updateNestedVarOne():void {
      SWFLoaded(_appLoaded).setVarOne("Updated varOne!");

  <mx:Label id="lbl"/>
  <mx:SWFLoader id="myLoader" width="300"
  <mx:Button label="Load SWF"
  <mx:Button label="Update Label Control in Outer Application"
  <mx:Button label="Update Nested Controls" click="updateNestedLabels();"/>
  <mx:Button label="Update Nested varOne" click="updateNestedVarOne();"/>


File Details
Created On Jul, 06, 2007 by Tracy Spratt
Last Modified On Jul, 06, 2007 by Tracy Spratt
Group: Tips and Articles
Flex Versions: 2.0
Category: General
Type: Complete Lesson
Difficulty: Intermediate