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));
      timesUpAlert.title = "TIME'S UP!"; 
      var testButton:Button = new Button();
      testButton.label = "FOO!";

      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





            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?

              You can have your button execute this code:




              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"

                            private function init():void {
                                btn.addEventListener(MouseEvent.CLICK, function ():void {btn.label = new Date().toTimeString()});

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




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



                  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"


                      <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:HBox id="TNTabSel">


                        import mx.controls.Label;
                        import mx.containers.VBox;
                        import mx.controls.Button;
                        private var i:int;


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

                        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();
                              var Buttn:Button = new Button();

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

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


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

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





                  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"/>




                  Can someone please help me?


                  Thanks alot.









                  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.