4 Replies Latest reply on May 29, 2008 1:09 AM by MasterOfAir

    Accessing and controlling windows

    pomo
      Currently stumped. I want to open and close windows from a main application window- this I have done with no issue. However, when I want to listen (from the main app) for a button event in said secondary window, I am coming up short. I can listen for button events and pass variables from the secondary window to the main window using Application.application, but I'd like to go in the other direction. Any suggestions? Thanks.
        • 1. Re: Accessing and controlling windows
          anirudhs Level 2
          Hi pomo,

          If the window you're going to open is based on mx:Window, you could create an instance of the actual class (not new Window(), instead new YourWindow()) and before doing a windowObj.open(), you can listen for the button's click event by doing a yourWindowObj.buttonName.addEventListener().

          Cheers,
          -----
          Anirudh Sasikumar
          http://anirudhs.chaosnet.org/
          • 2. Re: Accessing and controlling windows
            pomo Level 1
            Anirudh-- Thanks for the response. Sorry for being obtuse, but I need some clarification. Additionally, I want to ask another question that is related. First the clarification.

            Here is the main application script--
            <?xml version="1.0" encoding="utf-8"?>
            <mx:WindowedApplication xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" width="444" height="289" initialize="init(event)">
            <mx:Script>
            <![CDATA[
            import windowComponents.logIn;
            import mx.core.*;
            import flash.display.*
            import flash.events.Event;
            import flash.events.MouseEvent;
            import mx.controls.Alert;

            private var logInWindow:logIn

            public function init(e:Event):void{
            launchButton.addEventListener(MouseEvent.CLICK, launcher);
            }

            public function launcher(evt:MouseEvent):void{
            var logInWindow:Window = new logIn;
            logInWindow.logInButton.addEventListener(MouseEvent.CLICK, buttonResponse);
            logInWindow.open(false);
            logInWindow.nativeWindow.x = 200;
            logInWindow.nativeWindow.y = 300;
            logInWindow.activate();
            }

            public function buttonResponse(evt:MouseEvent):void{
            Alert.show("I heard that");
            }
            ]]>
            </mx:Script>
            <mx:Button x="188" y="47" label="Button" id="launchButton"/>
            <mx:TextInput x="141" y="161" id="nameDisplay" />
            <mx:TextInput x="141" y="191" id="passwordDisplay"/>

            </mx:WindowedApplication>

            Here is the window.mxml script--
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Window xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" width="266" height="204" >
            <mx:Button x="167" y="153" label="logIn" id="logInButton"/>
            <mx:TextInput x="26" y="57" id="logInName"/>
            <mx:TextInput x="26" y="87" id="logInPassword"/>

            </mx:Window>

            When I run the app, I get a 1119: Access of possibly undefined property logInButton through a reference with static type mx.core:Window. windowWork/src windowWork.mxml line 20

            Can you explain what is happening? Am I going about this the right way? My goal here is to try and centralize much of my code in the main application- using the windows as sort of utilities, like log in, accessing files, using sliders in one window to manipulate charts, and so on. So in the case of this little lab that I have set up, I'd like to launch the login from the main app, and collect the input data and send it back to the main app (doing this from centralized script in the main window.

            I guess this leads me the other question. Is centralizing the code still a good idea? Or is it better to make the individual windows or components, separate, self contained pieces that have the functionality/communication built in? What's your opinion?

            Thanks. -David

            • 3. Re: Accessing and controlling windows
              anirudhs Level 2
              Personally, I suggest that your should keep code related to each component within that component. Centralization will decrease modularity. Ideally, instead of your child components passing variables to the parent via Application.application, you should dispatch custom events to which your parents must be listening. This is so that communication only requires knowing the event name to listen to. Makes it easy to change your child component or plugin a different child component for the same purpose.

              Now, to the code!

              Firstly, instead of doing a new Window(), you should do a new logIn() (assuming that is the child window mxml file's name). That is:

              var logInWindow:logIn = new logIn();

              Secondly, I just realized that the components inside the window are only created once open() is called. So do the addeventlistener after calling open (unlike what I said in my earlier post):

              public function launcher(evt:MouseEvent):void{
              var logInWindow:logIn = new logIn ;
              logInWindow.open(false);
              logInWindow.logInButton.addEventListener(MouseEvent.CLICK, buttonResponse);
              logInWindow.nativeWindow.x = 200;
              logInWindow.nativeWindow.y = 300;
              logInWindow.activate();
              }
              • 4. Re: Accessing and controlling windows
                MasterOfAir
                Hi,
                I can not help you much as it is too technical but:
                1) can not you use like a HBox and play with "visible" property?
                Like you put your components on the HBox, you hide it when you click somewhere and you reshow it when you click somewhere else!?
                2) I see in your code you use a NEW window! is that a proper way to have two different..."screen", I mean like if you want lets say an introduction page with swf, images components, and then another page with different components?
                What would be the way to navigate between the two "windows", like playing with "visible"?

                Thanks!