PopUps in Flash & Flex

mx.managers.PopUpManager is a utility class in Flash MX 2004, Flash 8, Flex 1.5, and Flex 2 used to create popups. A popup is classified as a component that floats above the main application. Typically, Windows, Alerts, and ComboBox drop downs are used in this fashion. Developers utilize them for creating popup dialogues like preferences, login windows, etc.

The method createPopUp takes, usually, 3 parameters. The first is which window to pop over, the 2nd is the “thing” to create as a popup, and the 3rd is whether to make it modal or not. Modal being you have to close that popup before you can interact with any other component on the stage. Using alert() in JavaScript will generate a modal dialogue for example. Shutting down your computer on Windows XP shows a modal dialogue; you can only click one of the 4 buttons.

Typically, the first parameter in Flash is set to _root. While not the best choice, as long as your SWF is never loaded, it’s a satisfactory choice. You are better off using your top level component, typically the one that wraps your entire application. Your main application class can provide a reference to itself for this purpose, or you can only delegate the “power of creating popups” to it, thus it can pass “this” in as the first parameter. Using _root, however, when using the centerPopUp method, it’ll utilize the whole movie to find out where the center is, thus nicely positioning the window in the center.

_level0 isn’t so hot because if your SWF is loaded into another one, you cannot prevent the popups from “invading” your main movie via _lockroot. This is the common fix for the ComboBox component in Flash when it’s used in a SWF that is loaded. The dropdown no longer works because the PopUpManager digs his way up to find the “one true _root”.

Seriously, I’m not justifying the use of _root, but if you’re in a rush and aren’t loading movies, it’s an acceptable choice.

Using the component that launched the PopUpManager isn’t so hot either because centering can get really jacked up if your component you are using is itself off-centered when using centerPopUp. Granted, there are valid use cases.

Using _root in Flex, however, isn’t such a good idea. Using it in Flash is bad enough; it’s a hardcoded reference to something. While the practice of loading Flex applications into other ones is frowned upon (by me at least), using archaic Flash references in a more Object Orienated codebase will confuse other Flex developers, and there is another way.

Using MovieClip(mx.core.Application) instead of _root is a more “wow my code doesn’t look hardcoded even though it is” way. You and I know what _root is, but some other Flex developers might not. Additionally, the casting to MovieClip is just so the compiler will leave you alone. Perfect example of where OOP gets in the way.

…2nd time in this blog entry I’ve been tempted to suggest using _root…

Do not use mx.core.Application.application, that’ll crash Flex. All that is is a static getter/setter that returns _level0, but for some reason it crashes your SWF.

In Flex 2, they actually provide a nice new method for us, popUpWindow. A conveinance method much like the Flex 1.5’s alert, which allowed you to easily create alerts with less code.

Again, popups are not just dialogues and forms-in-your-face, they can be for component GUI controls, as well. Just the other day, I created the popup slider that you have in Photoshop and Fireworks for adjusting alpha values; the actual slider is a popup. On the other end of the spectrum, entire mini-applications can be used as popups as well. They are draggable when using containers like Panel & TitleWindow if you don’t make them modal.

7 Replies to “PopUps in Flash & Flex”

  1. Arrrghhhhh your thinking of using _root. saying OOP gets in the way? I dont know what to think, to agree in this instance or… or …. arrrrrrrrrgggghhhhh ;o)

  2. Do you manage to handle the depth of the windows that are being pop up by popupmanager in Flex 2? Since they removed swapDepth, I wonder wat are the ways to swap the depth of the windows pop up.

  3. That photoshop-like pop-up slider component sounds pretty sweet. Did you post it somewhere so other people can use it? Thanks!

  4. Even if I use _root, it seems that the popup window can only cover the components put on that stage at design time. All run time created components are sitting above the popup. Any idea how I can fix that? Thanks!

Comments are closed.