5 Replies Latest reply on Aug 19, 2009 2:18 PM by konsti22

    Adding a button and click handler with Actionscript?

    Handycam Level 1

      I have a pop-up window based on a Spark panel.  I want to add buttons to it with Actionscript.  I know how to add the button itself, label, etc, but can't find anything on adding the click event and handler.

       

      So far:

      timesUpAlert = com.TimesUpPanel(PopUpManager.createPopUp(this,com.TimesUpPanel,true));
      PopUpManager.centerPopUp(timesUpAlert);
      timesUpAlert.title = "TIME'S UP!"; 
      timesUpAlert.addEventListener("TRY_AGAIN",retryLevel);
      var testButton:Button = new Button();
      testButton.label = "FOO!";
      timesUpAlert.buttonGroup.addElement(testButton);
      

      Where "buttonGroup" is an HGroup in my component.

       

      I need to add a click event and handler that then I listen more in the parent application.

        • 1. Re: Adding a button and click handler with Actionscript?
          Flex harUI Adobe Employee

          testButton.addEventListener(MouseEvent.CLICK, someHandler)

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Adding a button and click handler with Actionscript?
            Handycam Level 1

            Thank you, perfect.

             

            Is there a way I can easily create a simple closebutton like

             

            testButton.addEventListener(MouseEvent.CLICK,

            PopUpManager.removePopUp(myAlertWindow));

             

            which doesn't work, without having to create a whole handler just to close

            the popup?

            • 3. Re: Adding a button and click handler with Actionscript?
              watern

              You can have your button execute this code:

               

              this.parent.removeChild(this);

               

              Although I do usually stay away from using "parent", it is a very simple technique.

               

              I hope that helps.

              • 4. Re: Adding a button and click handler with Actionscript?
                Peter deHaan Level 4

                Not sure how clean it is, but you could try an anonymous inline function:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                        xmlns:s="library://ns.adobe.com/flex/spark">

                    <fx:Script>
                        <![CDATA[
                            private function init():void {
                                btn.addEventListener(MouseEvent.CLICK, function ():void {btn.label = new Date().toTimeString()});
                            }
                        ]]>
                    </fx:Script>

                    <s:Button id="btn" label="Spark Button" initialize="init();" />

                </s:Application>

                 

                Peter

                • 5. Re: Adding a button and click handler with Actionscript?
                  konsti22

                  Hello,

                   

                  i'm new to establishing errohandlers in actionscript, and have a question that may be similar to the original one in his thread.

                   

                  i have a tabnavigator- and a HBox-body that i create in mxml and that i want to fil with panels and buttons at runtime via actionscript. This works fine so far.

                   

                  Here the code i use in short:

                   

                  ----------------------------------

                   

                  <?xml version="1.0"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                      creationComplete="init()">

                   

                      <mx:Panel title="TabNavigator Demo" height="90%" width="90%"
                          paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

                   

                          <mx:TabNavigator id="TN"  width="100%" height="100%">
                          </mx:TabNavigator>

                   

                          <mx:HBox id="TNTabSel">
                          </mx:HBox>

                   

                      </mx:Panel>
                     
                  <mx:Script>
                      <![CDATA[
                        import mx.controls.Label;
                        import mx.containers.VBox;
                        import mx.controls.Button;
                       
                        private var i:int;

                   

                        private function init():void{
                     ->   createLinkHandler();
                          initTabs(3);         
                        }

                        private function initTabs(TabCount:int):void{
                          
                            for (i=0; i<TabCount; i++) {
                              var Box:VBox = new VBox();
                              Box.label ="Tab " + (1+i).toString();
                              var Lbl:Label = new Label()   
                              Lbl.text ="Panel " + (1+i).toString();
                              Box.addChild(Lbl);
                              TN.addChild(Box);
                              var Buttn:Button = new Button();

                        ->   Buttn.id = i.toString();

                              Buttn.label ="choose Tab " + (1+i).toString();
                              TNTabSel.addChild(Buttn);
                            } 
                        }   

                   

                  ->   private function linkHandler(event:MouseEvent):void {
                  ->      TN.selectedIndex=(event.target.id);
                  ->   }


                  ->   private function createLinkHandler():void {
                  ->     TNTabSel.addEventListener(MouseEvent.CLICK,linkHandler);
                  ->   }

                   

                      ]]>
                  </mx:Script>
                     
                     
                  </mx:Application>

                  ---------------------------------------

                   

                  My Problem now is, that the buttons do nothing when i click on them, as they would do when i programm the buttons in mxml
                  , much like in this code:

                   

                  ................

                   

                          <mx:HBox id="TNTabSel">
                              <mx:Button label="Select Tab 1" click="TN.selectedIndex=0"/>
                              <mx:Button label="Select Tab 2" click="TN.selectedIndex=1"/>
                              <mx:Button label="Select Tab 3" click="TN.selectedIndex=2"/>
                          </mx:HBox>

                   

                  ....................

                   

                  Can someone please help me?

                   

                  Thanks alot.

                   

                  konsti

                   

                  -------


                  EDIT

                   

                  Hi,

                   

                  i tried a slightly different approach and, viola, it works like i want.

                  I marked the new inserted Lines with "->".

                   

                  Maybe someone has use for it.

                   

                  Thanks again.

                   

                  konsti