5 Replies Latest reply on Mar 3, 2010 4:00 AM by Loic.Aigon

    adding a button by clicking on a "Add button" button

    Loic.Aigon Adobe Community Professional

      Hi,

       

      I want to add dynamically button controls on my application. These buttons will be created when the user click on a "Add button" button.

       

      What command should I use to get that result ?

       

      Using <mx:button...> into the [[CDATA... doesn't work and all the trys I did based on what I grabbed on the net didn't run neither.

       

      Thanks a lot for any input.

       

      Loic

        • 1. Re: adding a button by clicking on a "Add button" button
          Gregory Lafrance Level 6

          This should answer your question:

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
              <![CDATA[
                import mx.controls.Alert;
                import mx.controls.Button;
                
                private function addButton():void{
                  var btn:Button = new Button();
                  btn.label = "New Button";
                  btn.addEventListener(MouseEvent.CLICK, clickFunc);
                  vb.addChild(btn);
                }
                
                private function clickFunc(evt:MouseEvent):void{
                  mx.controls.Alert.show(evt.currentTarget.toString());
                }
              ]]>
            </mx:Script>
            <mx:Button label="Add Button" click="addButton();"/>
            <mx:VBox id="vb"/>
          </mx:Application>
          


          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          • 2. Re: adding a button by clicking on a "Add button" button
            Loic.Aigon Adobe Community Professional

            Hi Greg,

             

            Thank you for your great input.

            However, Flex throws an error when I try that.

             

            var btn:Button = new Button(); > "This type can't be found or isn't a compiling constant" (Frenglish translation sorry)

             

            I am using patchpanel technology and here is the code. Maybe this is because I want to mix both import com.adobe.cs3.InDesign and mx.controls.* stuff ?

             

            Here is my code so far if it can help understanding :

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="325" height="254" creationComplete="onCreationComplete()">
                 <mx:Script>
                      <![CDATA[
                           //Importe le DOM Indesign
                           import com.adobe.cs3.InDesign.*
                           import mx.controls.Alert;
                           import mx.controls.Button;
                 
                           /*
                                Fonction appelée quand l'utilisateur appuie sur le bouton 'start'
                           */
                           public function onClick():void
                           {
                                // Creation d'un bouton
                                //myButton = new Button();
                                //yButton.label = "Hello world !";
                                //InDesign.alert("ttt");
                                var btn:Button = new Button();
                              btn.label = "New Button";
                              btn.addEventListener(MouseEvent.CLICK, clickFunc);
                              vb.addChild(btn);
                           }
                           
                           private function clickFunc(evt:MouseEvent):void{
                                mx.controls.Alert.show(evt.currentTarget.toString());
                           }
                           
                           /*
                                Fonction de recadrage du fichier swf après son initialisation
                           */
                           public function onCreationComplete():void
                           {
                                InDesign.$resize( width, height);
                           }
                      ]]>
                 </mx:Script>
                 <mx:Button x="135" y="119" label="start" click="onClick()"/>
                 <mx:Button label="Add Button" click="addButton();"/>
                   <mx:VBox id="vb"/>     
            </mx:Application>
            

             

            Thanks anyway for your first input and hoping you could help me fixing that issue.

             

            Regards,

             

            Loic

            • 3. Re: adding a button by clicking on a "Add button" button
              Sebastien V. Level 3

              This probably not related to your issue but your "Add Button" is calling an addButton() method that does not exist.

               

              If you think the problem comes from the imports you can try instanciating your Button with

               

              var btn:Button = new mx.controls.Button();
              

               

              and see what the Flex compiler tells you then.

              • 4. Re: adding a button by clicking on a "Add button" button
                Loic.Aigon Adobe Community Professional

                Hi and thank you a lot for your care,

                 

                I tried what you offered and still bugs for an error.

                However, I could understand that flex was trying to initiate the button on the com.adobe.cs3.InDesign.* package and not on the mx.controls.Button one.

                 

                Thanks again.

                 

                Loic

                • 5. Re: adding a button by clicking on a "Add button" button
                  Loic.Aigon Adobe Community Professional

                  Hi Greg,

                   

                  I just rechecked and your solution is perfectly efficient in a web environnment.

                  It hust fails when I try to use it in mix with the cs3.swc library.

                   

                  Thanks a lot though.

                   

                  Loic