Skip to main content
Known Participant
February 22, 2018
Answered

How to Move UI button or any element in x and y axis specifically after creation?

  • February 22, 2018
  • 1 reply
  • 549 views

//Below script generates Window,Panel with button

//I would like to move button/UI element in x and y axis to desired location

//Could someone please let me know to achieve it?

function myScript(thisObj){

    function myScript_buildUI(thisObj){

       

        var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "tools",undefined ,{resizeable:true});

    

        

          var resAbout="panel{orientation:'row',\

                                          t1:Button{},\

                                           \

                                          \

                                          }";     

                                         

                myPanel.grp=myPanel.add(resAbout);

    myPanel.preferredSize=[500,200];

   myPanel.grp.preferredSize=[500,200];

           myPanel.grp.margins=2;

          

  

return myPanel;

}

var myScriptPal = myScript_buildUI(thisObj);

    if((myScriptPal != null) && (myScriptPal instanceof Window)) {

        myScriptPal.center();

        myScriptPal.show();

    }

}

      myScript(this);

This topic has been closed for replies.
Correct answer Tomas Sinkunas

Button object as a location property that you can modify. Take a look at this snippet:

(function (thisObj) {

    buildUI(thisObj);

    function buildUI(thisObj) {

        var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "script", undefined, {

            resizeable: true

        });

        // Resize the pallete

        win.preferredSize = [400, 200];

        // Create a dummy button

        var btn = win.add("button", undefined, "OK");

        btn.onClick = function () {

            // Generate rnadom X and Y values

            var randomX = Math.random() * 400;

            var randomY = Math.random() * 200;

            // Change buttons position

            this.location = [randomX, randomY];

        }

        // Handle window resizing

        win.onResizing = win.onResize = function () {

            this.layout.resize();

        };

        // Show panel

        if (win instanceof Window) {

            win.center();

            win.show();

        } else {

            win.layout.layout(true);

            win.layout.resize();

        }

    }

})(this);

1 reply

Tomas Sinkunas
Tomas SinkunasCorrect answer
Legend
February 24, 2018

Button object as a location property that you can modify. Take a look at this snippet:

(function (thisObj) {

    buildUI(thisObj);

    function buildUI(thisObj) {

        var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "script", undefined, {

            resizeable: true

        });

        // Resize the pallete

        win.preferredSize = [400, 200];

        // Create a dummy button

        var btn = win.add("button", undefined, "OK");

        btn.onClick = function () {

            // Generate rnadom X and Y values

            var randomX = Math.random() * 400;

            var randomY = Math.random() * 200;

            // Change buttons position

            this.location = [randomX, randomY];

        }

        // Handle window resizing

        win.onResizing = win.onResize = function () {

            this.layout.resize();

        };

        // Show panel

        if (win instanceof Window) {

            win.center();

            win.show();

        } else {

            win.layout.layout(true);

            win.layout.resize();

        }

    }

})(this);