5 Replies Latest reply on Mar 8, 2011 6:53 PM by JJ Fulks

    Array of Button elements

    JJ Fulks Level 1

      Is it possible to have an array of Button elements in a window, and if so how would one write a single onClick event hanlder to recognize which Button element of the array of Buttons was clicked?

        • 1. Re: Array of Button elements
          JJ Fulks Level 1

          Or, can one write an inline call to a specific function within the statement that adds the Button to its container?

          • 2. Re: Array of Button elements
            Marc Autret Level 4

            There are several approaches. Here is one:

             

            var NUMBER_OF_BUTTONS = 8;
            
            var dlg = new Window('dialog', "Array of Buttons Demo"),
                 pn = dlg.add('panel', undefined, "Buttons");
            
            var myClickHandler = function(ev)
                 {
                 var btn = ev.target,
                      ps = btn.properties;
                 if( !ps.hasOwnProperty('idx') ) return;
                 alert( "You clicked Button #" + ps.idx );
                 };
            
            for( var i=0 ; i<NUMBER_OF_BUTTONS ; ++i )
                 pn.add('button', undefined, "Button"+i, {idx:i});
            
            pn.addEventListener('click', myClickHandler);
            
            dlg.show();
            

             

            The interesting point is that you can inject your own creation properties while adding a widget —here I arbitrarily use idx to backup each button index. You also could use the regular name creation property (String).

             

            @+

            Marc

             

            Edit: My code does not use any actual JS array to handle the buttons, but of course you may create a btns var — var btns = []; — and then use:

            btns[i] = pn.add('button', undefined, "Button"+i, {idx:i});

            if you need to target the buttons this way.

            • 3. Re: Array of Button elements
              JJ Fulks Level 1

              Thanks Marc, that helps a lot! I am going to allow the user to dynamically create buttons, on the fly ... an array made sense, but seeing how your method adds a new property to each button to uniquely identify each one, idx in the case of your example, seems to make for a more simple, more elegant way of accomplishing what I'm trying to do.

               

              In regards to refreshing the window to show the updated interface each time a button is added, what would be the proper way to accomplish this without having the initial window/container dimensions reset?

               

              I appreciate the help!

               

              JJ

              • 4. Re: Array of Button elements
                Marc Autret Level 4

                JJ_Fulks wrote:

                 

                (...)

                In regards to refreshing the window to show the updated interface each time a button is added, what would be the proper way to accomplish this without having the initial window/container dimensions reset?


                 

                Again I see different approaches:

                 

                1) You can add the maximum number of buttons at creation time — provided that the user is not allowed to create an infinite number of buttons ;-) — and hide the widgets until you need it to appear.

                 

                2) Also you can dynamically add buttons in your container at execution time.

                Unless you explicitly set the size of the container area  to receive the maximum number of widgets, this generally causes a layout problem. The usual solution is to call the layout() method of the Layout manager: myContainer.layout.layout(true)

                Bob Stucky offered a sample code here: http://forums.adobe.com/message/2280793#2280793

                 

                3) Finally (or additionaly) you can use a ScriptUI scrollbar in order to avoid the container to be resized. A few examples of ScriptUI scrollbars have been posted in this forum but I can't find them now...

                 

                @+

                Marc

                • 5. Re: Array of Button elements
                  JJ Fulks Level 1

                  Marc, thanks for the suggestions, I really appreciate it! I played around a little with your methods of adding buttons dynamically ....  thanks to your examples, I made some good progress on my d

                  I think adding a scrollbar may be a little too difficult for me to do now, but a scrollbar would certainly solve my dilemma .... I may attempt it after I get a little more familiar with ScriptUI.

                   

                  And thanks for the hint on not allowing users to add an infinite number of buttons ... I can definitely see the problems associated with that.

                   

                  Time to finish the planning and design ... .thanks again for your help!