6 Replies Latest reply on May 24, 2009 1:29 PM by hsfrey

    Can't address or absolutely position a Panel at runtime

    hsfrey

      As far as I can see, I can't do what I want to do in Flex. Am I wrong?

       

      I want to place many panels in an Application to model the structure of an XML tree structure read in at runtime.

       

      I would need to set the position of each new panel with respect to previous panels.

       

      This means I would have to read the position of the previously created panels.

       

      In order to do that I would need their ID in order to address them.

       

      To do this I would need to construct an ID for each panel at runtime, but apparently this can't be done in Flex.

       

      Even if it could be, I see no way to specify absolute positioning with an addChild call.

       

      So, is this impossible in Flex?

       

      I considered a Tree control, but it looks to me as though it uses only icons, not components, like Panels.

      And my tree structure is multiply rooted, and I don't see how a Tree control could handle that.

       

      So it looks like I'd have to do the whole thing in Graphics mode and have to rewrite all the Flex goodies from scratch,

      which would vitiate all the advantages of using Flex.

       

      Any suggestions?

        • 1. Re: Can't address or absolutely position a Panel at runtime
          _Natasha_ Level 4

          Hi,

           

          you can do all you've described.

           

          The first is about ID. You can set all properties at runtime. For example,

          var btn:Button = new Button();

          btn.id = "myButton1";

          this.addChild(btn);

          this["myButton1"].label = "My Button"; // the way to get control by id.

           

          The second is about positioning.

          addChild() function don't have any parameters for positioning. It depends on properties of controll you've added and control you added to.

          Each control have properties: x, y, top, bottom, left, right. So you can set what you want.

          In some controls, such VBox, HBox, place childs in a special way. See docs for details.

           

          Hope this helpful.

          • 2. Re: Can't address or absolutely position a Panel at runtime
            Muzak Level 3

            You don't really need an id though. A reference to the component will do.

             

            var btn:Button = new Button();

            btn.id = "myButton1";

            this.addChild(btn);

            this["myButton1"].label = "My Button"; // the way to get control by id.

             

            The following will do the same as above, without the use of an id

             

            var btn:Button = new Button();
            btn.label = "My Button";
            addChild(btn);
            
            
            • 3. Re: Can't address or absolutely position a Panel at runtime
              hsfrey Level 1

              Natasha:

               

                   Yes, thank you. I believe that is at least part of the answer.

               

                   I couldn't do it in MXMXL, so I have to think more in AS.

               

                   In MXML I tried to set the ID by binding it to a variable, and the compiler said that was illegal to use a variable. Then I found the docs which said that the ID could not be changed at runtime, so I assumed it had to be set with a constant. But I guess that "changing" was not meant to include "setting".

               

                As for positioning, the 'child' I want to add is a panel, and I want to add it to the Application, in an absolute position which I'll have to compute.

              The 'addChild' function doesn't include arguments for absolute positioning, but if I'm using AS perhaps I can set the position after I've made the addChild call.

               

                   I'll try it. Thanks.

               

              Harvey

              • 4. Re: Can't address or absolutely position a Panel at runtime
                hsfrey Level 1

                Muzak:

                 

                     Thanks for the response.

                 

                     In your example you initialize button.id with a constant string.

                I tried to do it in MXML with a bound variable and got a compiler error.

                 

                     The reason I need an id, is that I need to position a panel in relation to a previous panel, so I need the id of the previous panel so I can retrieve its position and size to calculate the position of the new panel. And I don't know that id till runtime.

                 

                Harvey

                • 5. Re: Can't address or absolutely position a Panel at runtime
                  Muzak Level 3

                       In your example you initialize button.id with a constant string.

                  I tried to do it in MXML with a bound variable and got a compiler error.

                  No I didn't.. I took the code someone else posted, and posted code how to do the same without the use of an id.

                   

                       The reason I need an id, is that I need to position a panel in relation to a previous panel, so I need the id of the previous panel so I can retrieve its position and size to calculate the position of the new panel. And I don't know that id till runtime.

                   

                  Again, you (most likely) don't need id's at all. You just need a reference to the "previous" panel.

                  There are numerous ways to do that (well at least two).

                  One is to store a reference to the previous panel in a variable, another is to use the DisplayObjectContainer API to get to the "last added child child".

                  For the latter you can use:

                   

                  var lastChild:DisplayObject = DisplayObjectContainer.getChildAt(DisplayObjectContainer.numChildren-1);

                  You'll have to replace "DisplayObjectContainer" with whatever it is you added the Panel instances to.

                  Have a look at the DisplayObjectContainer API in the docs.

                  • 6. Re: Can't address or absolutely position a Panel at runtime
                    hsfrey Level 1

                    Muzak:

                     

                         I can't find a reference to "reference" in my growing Flex library or online.

                    If it means something like a C "pointer", I'd love to have it. C is my native language. <G>

                     

                         I can't get by with just a pointer to "previous", since, as I mentioned, my tree is multiply rooted, and nodes won't necessarily be added in strict top to bottom order.

                     

                         But, in poking around I discovered that I can get children by NAME (which presumably isn't runtime restricted by the compiler as ID is, or by Index.

                    I could store the indexes in each node, and traverse the tree in any direction.

                     

                         So, thanks. I see the problem is not insoluble. I'll play with it.

                     

                    Harvey