11 Replies Latest reply on Sep 28, 2009 10:47 AM by zakabalt

    Create a button in one component and use it in other component

    zakabalt

      Hello, I have a problem(is not the first time but I don't have very clear that problem and I'm asking again). I'm creating a button in one component doing that

       

      Comp1:

      public var btn=Button;

       

      override public function main(args:IParams):void{

       

           var btn = new Button

          btn.label = ....

             .......

      }

       

      And now I want to use this button in another component:

       

      Comp2:

       

      private var dia:Object; //It's my own object, it's a Dialog but with my properties,a dn the dialog need (button,string,boolean)

       

      private function onClick(event:Event):void{
                      dia = new Object(HERE I HAVE TO PASS THE BUTTON , "Tittle", false);

      }

       

      I want know how can use the button that I have declared in the first component in my object. It's much more complicate but I have tried simplify my code.

       

      I hope that somebody can help me.

       

      Thanks!

        • 1. Re: Create a button in one component and use it in other component
          _Natasha_ Level 4

          Hi,

          question is clear, but not clear links between Comp1 and Comp2?

          What component has an object of what class?

          • 2. Re: Create a button in one component and use it in other component
            zakabalt Level 1

            comp1 is a Flex module and comp2 is a Flex component. I don't know if you're asking that,

            • 3. Re: Create a button in one component and use it in other component
              zakabalt Level 1

              To understand better. I have this module and I want to divide in one module and in one component. I want one module with the main, and then one component whit the onClick where I use the win that I have created in the main. I HAVE TO divide this code in two files.

               

              <?xml version="1.0" encoding="utf-8"?>
              <AbstractVisualModuleExecutable
                  xmlns:mx="http://www.adobe.com/2006/mxml">
                 
                  <mx:Script>
                      <![CDATA[
                         
                          private var win:ITopComponent; //My window
                          private var dia:Dialog; //My dialog
                         
                          override public function main(args:IParams):void{
                         
                              win = initContext.environment.createTopComponent(); //I'm creating the window
                              win.title = "Test"
                              win.contentPane = this;
                              win.visible = true;
                             
                             
                          }
                         
                          private function onClick(event:Event):void{
                              dia = new Dialog(win, "Titulo", false);
                             
                              var btn2:Button = new Button();
                              btn2.label = "Close";
                              btn2.addEventListener(MouseEvent.CLICK, function(event:Event):void{
                                  dia.dispose();
                              });
                             
                              dia.contentPane.addChild(btn2);
                              dia.visible = true;
                          }
                         
                      ]]>
                  </mx:Script>
                 
                  <mx:Button id="btn" click="onClick(event)" label="Dialog" />
              </AbstractVisualModuleExecutable>

              • 4. Re: Create a button in one component and use it in other component
                zakabalt Level 1

                Nobody can help me after rethink my question? Forget the first post and the buttons. I only want to divide my code in two like I said in the last post. I think that it isn't very dificult but I don't know how can I do

                 

                Thanks!

                • 5. Re: Create a button in one component and use it in other component
                  _Natasha_ Level 4

                  May be it's really not difficult, but it's not clear what you want.

                   

                  As an example (psevdo code),

                   

                  <Comp1>

                  <Button id="btn"/>

                  </Comp1>

                   

                  <Comp2>

                       <Script>

                       // here you can get btn

                       Button = cmp1.btn //this is a button

                      // BUT if you don't have a instance of Comp1 in component you can't get any components from it

                       // Also you can get a component throw parent links.

                       </Script>

                       <Comp1 id="cmp1"/>

                  </Comp2>

                  • 6. Re: Create a button in one component and use it in other component
                    zakabalt Level 1

                    I want to do that but I want to instance Comp1 in the Script, not after the Script. I'm doing cmp1:Component1 and then cmp1.btn but I get a null object. What are I doing wrong?

                    • 7. Re: Create a button in one component and use it in other component
                      _Natasha_ Level 4

                      Now it's clear.

                       

                      When you write:

                      var myComp:Comp1 = new Comp1();

                      only not visible properties are available to use. For example, sting variables...

                       

                      Constructor doesn't create visible children! There is a special function createChildren() for all components.

                      Visible controls will start to create only when you call

                      this.addChild(myComp);

                       

                      Buttons is available to use only after myComp raise event creationComplete.

                       

                      Hope, this helpful.

                      • 8. Re: Create a button in one component and use it in other component
                        zakabalt Level 1

                        I think that I have understood that you said, but I can't get do that with my code. Can you put it a example dividing my code in 2? Forget the buttons, use the code that I have put. I only want use 'win' of the main of the first component, in the function onclick of the second component.

                         

                        I know that I'm asking a lot, but I don't know if I'm stupid or I have a very bad day, but I don't get and I need. I'm sorry.

                         

                        Thanks for all your patience

                        • 9. Re: Create a button in one component and use it in other component
                          _Natasha_ Level 4

                          <Dialog>

                          <Button id="btn" click="this.dispose()"/> <!-- this is enough for calling only dispose method -->

                          </Dialog>

                           

                          <AbstractVisualModuleExecutable
                              xmlns:mx="http://www.adobe.com/2006/mxml">
                             
                              <mx:Script>
                                  <![CDATA[
                                     
                                      private var win:ITopComponent; //My window
                                      private var dia:Dialog; //My dialog
                                     
                                      override public function main(args:IParams):void{
                                     
                                          win = initContext.environment.createTopComponent(); //I'm creating the window
                                          win.title = "Test"
                                          win.contentPane = this;
                                          win.visible = true;
                                         
                                         
                                      }
                                     
                                      private function onClick(event:Event):void{
                                          dia = new Dialog(win, "Titulo", false);
                                         
                                          var btn2:Button = new Button();
                                          btn2.label = "Close";
                                          btn2.addEventListener(MouseEvent.CLICK, function(event:Event):void{
                                              dia.dispose();
                                          });
                                         
                                          dia.contentPane.addChild(btn2);

                                        dia.addEventListener(FlexEvent.CREATION_COMPLETE, onCreateDialog);

                                         dia.visible = true;
                                      }

                          private function onCreateDialog(event:FlexEvent):void

                          {

                               dia.btn.addEventListener(MouseEvent.CLICK, anyFunctionForHandling);

                          }
                                     
                                  ]]>
                              </mx:Script>
                             
                              <mx:Button id="btn" click="onClick(event)" label="Dialog" />
                          </AbstractVisualModuleExecutable>

                          1 person found this helpful
                          • 10. Re: Create a button in one component and use it in other component
                            zakabalt Level 1

                            I'm sorry, probably I'm explainign wrong, I don't know if it's for my english or because I don't know how to say, but this is not the problem. I will put the code because I think that ou could understand better. Forget the buttons, the problem is the window parameter

                             

                            I'm very sorry because you're wasting time with me, but I hope that you can do the last try now with the code. If you don't understand something ask me before start thinking.

                             

                            MyModule

                            <?xml version="1.0" encoding="utf-8"?>
                            <AbstractVisualModuleExecutable
                                xmlns:mx="http://www.adobe.com/2006/mxml">
                               
                                <mx:Script>
                                    <![CDATA[
                                       
                                        private var win:ITopComponent; //MY window
                                        private var dia:Dialog; //My Dialog
                                       
                                        override public function main(args:IParams):void{
                                       
                                            win = initContext.environment.createTopComponent();
                                            win.title = "Prueba Dialog"
                                            win.contentPane = this;
                                            win.visible = true;             
                                           
                                        }
                                       
                                        private function onClick(event:Event):void{
                                            dia = new Dialog(win, "Title", false); ((Here I use the window, because I have to say to the dialog who is her father. Win is the father or the dialog, the dialog will create when I do click in one button of my window(the father).
                                           
                                            var btn2:Button = new Button();
                                            btn2.label = "Cerrar";
                                            btn2.addEventListener(MouseEvent.CLICK, function(event:Event):void{
                                                dia.dispose();
                                            });               
                                            dia.contentPane.addChild(btn2);
                                        }
                                       
                                    ]]>
                                </mx:Script>
                               
                                <mx:Button id="btn" click="onClick(event)" label="Dialog" />
                            </AbstractVisualModuleExecutable>

                             

                            Now I want to divide this applicatión in one module and one component.

                             

                            MyNewModule (something like this):

                             

                            <mx:Script>
                                    <![CDATA[
                                       
                                        private var win:ITopComponent;
                                        private var dia:Dialog;
                                       
                                        //Here I create the window

                                        override public function main(args:IParams):void{
                                       
                                            win = initContext.environment.createTopComponent();
                                            win.title = "Prueba Dialog"
                                            win.contentPane = this;
                                            win.visible = true;               
                                        }

                            </Script>

                             

                            MyNewComponent (something like this):

                            <Script>

                                      private var dia:Dialog;

                             

                                     Here the instance of win I supose, but I don't know how. I have tried more than one thing

                             

                                      private function onClick(event:Event):void{

                                            dia = new Dialog(here the win, "Title", false); //here I want to use the win that I have created in MyNewModule, but I only get a null when I execute the module. I don't know how can I do, instance.top or something like this I suposse it.
                                           
                                           //This part is not important

                             

                                            var btn:Button = new Button();
                                            btn.label = "Cerrar";
                                            btn.addEventListener(MouseEvent.CLICK, function(event:Event):void{
                                                dia.dispose();
                                            });
                                            dia.contentPane.addChild(envio); //here I add the content of the dialog
                                            //dia.contentPane.addChild(btn); //here I add only a button without content, but don't worry about that, like I said later this is not important. The import thing is how can I use win when I create the dialog.
                                            dia.visible = true;
                                        }       
                            </Script>

                             

                            <mx:Button id="toOpenDialod"  label="Open"  click="onClick(event)"/>

                             

                            THANKS!

                            • 11. Re: Create a button in one component and use it in other component
                              zakabalt Level 1

                              I have tHe solution, I have create a class to organize my components and now it works correctly.

                               

                              Thanks for your help!