The following example shows how you can center a pop up window (in this case an Alert control) when the Flex application is resized by using the resize event on the <mx:Application /> tag and using the static PopUpManager.centerPopUp() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/06/centering-a-pop-up-window-when-a-flex-application-is-resized/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        resize="application_resize(event);">

    <mx:Script>
        <![CDATA[
            import mx.events.ResizeEvent;
            import mx.controls.Alert;
            import mx.managers.PopUpManager;

            private var alert:Alert;

            private function init():void {
                alert = Alert.show("The quick brown fox jumped over the lazy dog.", "title");
            }

            private function application_resize(evt:ResizeEvent):void {
                if (alert) {
                    PopUpManager.centerPopUp(alert);
                }
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Launch!" click="init();" />
    </mx:ApplicationControlBar>

</mx:Application>

View source is enabled in the following example. | Click here to open example in a new window

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

14 Responses to Centering a pop up window when a Flex application is resized

  1. eternalko says:

    Nice

  2. Andres says:

    Hi,

    Good example, but is it possible to use this kind of popup to enter information, or is it just for messages? If not, what would you recommend for user input?

  3. peterd says:

    Andres,

    I would probably use the TitleWindow container and PopUpManager class.

    Peter

  4. Andres says:

    Thanx! Those were exacly the classes I was looking for!

    Great Examples!!!

  5. Shane says:

    Nice solution. Worked well for me. Thanks!

  6. Abhilash says:

    I have a form designed using Flex. The form does not fit the screen area and hence I am having scroll bars in it. When we click on some of the controls in the form, we open popups. But when the popup is opened, if we scroll the form using the form scroll bar then the popup is not getting scrolled. I want the popup to be scrolled along with the form elements. Please let me know how this can be done.

    Thanks

    Abhilash

  7. David says:

    I have the same problem as Abhilash. Anyone have any answers?

  8. Anonymous says:

    I too have the same problem. Is there any solution..?

  9. Marcelo Salinas says:

    Nice example !!!.

    One Comment, the correct phrase is “The quick brown fox jumps over the lazy dog”

  10. Anonymous says:

    I created a login window,its a pop up window. How do i enable the scroll bar for pop up window(i.e login window) when i resize the browser?

    Suman

  11. Tom says:

    Lol !!! Woa !!! This code creates a big dependence between Application and the popup ….

    You must add this event listener on the popup component (use creationComplete=”Application.application.addEventListener(Res….”) and do the PopupManager.center(this); in the popup component else your code is working only for 1 Popup (good for a blog but useless for dev. )

    eBuildy, Web 2 Specialists

  12. ntaka says:

    nice
    check this link for centering a pop up window

  13. ntaka says:

    nice
    check this link for centering a pop up window http://www.ntaka.co.za/centering-a-popup-in-flex

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree