FLEX/AIR Dispatch Custom Events from Popup

Here's some kind of tutorial, just because I just needed it but couldn't find a howto for what I needed.
The problem was that I wasn't able to dispatch a custom event from a Popup and catch it in the main-mxml. Here we go:

Communicating with a PopUp is a common task. This article explores how to do that.
Here, a TitleWindow is used to present a choice of option. A button opens the Popup where the user can choose his option, or do other stuff ...

This is the code for the pop-up, OptionChooser.mxml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
  3. width="338" height="252"
  4. title="Choose Your Theme"
  5. showCloseButton="true"
  6. horizontalAlign="center"
  7. verticalAlign="middle"
  8. close="PopUpManager.removePopUp(this)">
  9.  
  10. <mx:Metadata>
  11. [Event(name="changeOption", type="option.OptionEvent")]
  12. </mx:Metadata>
  13.  
  14. <mx:Script>
  15. <![CDATA[
  16. import option.OptionEvent;
  17. import mx.managers.PopUpManager;
  18.  
  19. private function dispatchOptionChoice( optionName:String ) : void
  20. {
  21. dispatchEvent( new OptionEvent(optionName) );
  22. PopUpManager.removePopUp(this);
  23. }
  24. ]]>
  25. </mx:Script>
  26.  
  27. <mx:VBox horizontalAlign="left" verticalGap="17">
  28. <mx:RadioButton label="option1" click="dispatchOptionChoice('option1')"/>
  29. <mx:RadioButton label="option2" click="dispatchOptionChoice('option2')"/>
  30. <mx:RadioButton label="option3" click="dispatchOptionChoice('option3')"/>
  31. </mx:VBox>
  32.  
  33. </mx:TitleWindow>
  34.  

When a RadioButton is clicked, an event is dispatched. The event is a OptionEvent - a custom event type. This is the code for the event, OptionEvent.as:

  1. package option
  2. {
  3. import flash.events.Event;
  4.  
  5. public class OptionEvent extends Event
  6. {
  7. public static const CHANGE_OPTION:String = "changeOption";
  8. public var optionName:String;
  9.  
  10. public function OptionEvent(optionName:String, bubbles:Boolean=false, cancelable:Boolean=false)
  11. {
  12. super(CHANGE_OPTION, bubbles, cancelable);
  13.  
  14. this.optionName = optionName;
  15. }
  16. }
  17. }

This is the code for the main application to demonstrate it:

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  4. borderStyle="solid"
  5. borderColor="black"
  6. borderThickness="1">
  7.  
  8. <mx:Script>
  9. <![CDATA[
  10. import option.OptionEvent;
  11. import option.OptionChooser;
  12. import mx.managers.PopUpManager;
  13. import mx.core.IFlexDisplayObject;
  14.  
  15. private function showOptionDialog() : void
  16. {
  17. var pop:IFlexDisplayObject = PopUpManager.createPopUp(this, option.OptionChooser, true);
  18. pop.addEventListener( OptionEvent.CHANGE_OPTION, selectOption );
  19. PopUpManager.centerPopUp(pop);
  20. result.text = ""; // reset
  21. }
  22.  
  23. private function selectOption( event:OptionEvent ) : void
  24. {
  25. result.text = event.optionName;
  26. }
  27. ]]>
  28. </mx:Script>
  29.  
  30. <mx:Button x="24" y="27" label="Select Option" click="showOptionDialog()"/>
  31. <mx:Label x="24" y="57" fontWeight="bold" color="#FFFFFF" id="result"/>
  32.  
  33. </mx:Application>
  34.  

A custom event seems to be the easiest solution for this 'problem'. It can even contain more information specific for your App but also contains the basic information from an Event because it Extends from the Event-class. If you have a lot of data (e.g., a user profile) consider creating a class to represent it and have the custom event contain an instance of the class. This also makes it easier to change the information later.

Source files

Download Zip-file

This zip file is a Flex Builder 3 project archive. If you do have Flex Builder 3, you can import the source files directly from the zip.

Share this:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • LinkedIn
  • TwitThis
  • Live
  • E-mail this story to a friend!

5 Comments so far

  1. Gaiseecax on January 21st, 2009

    I am unable to understand this post. But well some points are useful for me.

  2. Buy acai berry on May 22nd, 2009

    Your blog is very interresting for me, i will come back here..

  3. andy matthews on August 26th, 2009

    Just FYI, the link to the ZIP file is wrong. You have a \; between your name instead of a period:

    matthijs\;stichelz instead of matthijs.stichelz.

    Thanks for the post, I’m trying your sample code right now.

  4. Matthijs on August 26th, 2009

    Thanks !!
    Link is fixed , enjoy the example!!

  5. samrat on June 1st, 2010

    Its really very cool solution.I got my problem solved.Very very thanks to author…

Leave a Reply