Position JavaFX Button in a specific location

25,314

If you want to specify the exact co-ordinates of your node, you can use a Pane instead of StackPane.


Your button, if added to a StackPane or similar layout which supports alignment, must use the translate properties to move the button. You cannot use setLayoutX() or setLayoutY() with these layouts.

Try using the following command to move the button from its initial location :

button.setTranslateX(10);
button.setTranslateY(20);
Share:
25,314
Amit
Author by

Amit

Updated on June 05, 2020

Comments

  • Amit
    Amit about 4 years

    My Question is how can i position a javafx button in a specific location.

    All the time that i tried to do this simple code is resulting that the Button is only located in the Center of the screen and not on my desired location.

    (I'm using StackPane)

    Code:

    Button button = new Button();
    
    button.setLayoutX(x);
    button.setLayoutY(y);
    

    Thanks in advance ,

    Amit.

  • Amit
    Amit about 9 years
    I'm afraid it is still not working and yes I'm using StackPane and Thanks for replying
  • ItachiUchiha
    ItachiUchiha about 9 years
    Translate will move the node from its current location by the specified value and not place it on the exact X, Y co-ordinate.
  • Amit
    Amit about 9 years
    So what do i need to do to make it on the exact X and Y of my desired co-oridnate?
  • James_D
    James_D about 9 years
    Use a Pane instead of a StackPane
  • ItachiUchiha
    ItachiUchiha about 9 years
    Use a Pane, as stated by @James_D