You can change the positioning of the elements in a HBox or VBox with in a scene.
Example hbox.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<HBox fx:id="hBox" prefHeight="213.0" prefWidth="473.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1" fx:controller="HboxController">
<TextField fx:id="textfield1" alignment="CENTER"/>
</HBox>
Example HboxController.java
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import java.net.URL;
import java.util.ResourceBundle;
public class HboxController implements Initializable {
@FXML
private HBox hBox;
@FXML
private TextField textfield1;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
hBox.setAlignment(Pos.CENTER);
}
}
This will center the elements in the HBox.
hbox.setAlignment(Pos.Center);
Also these links might be of help to you too
JavaFX center Button in VBox,
SetAlignment method in JavaFX
If you are trying to get a particular node that is an element of the hbox, you can either give it an id and refer to it that way or this link might be helpful otherwise
How do I get all nodes in a parent in JavaFX?
Scene
has a root of typeParent
. Everything in theScene
is a descendant of thatParent
. You need to use the proper layout, or combination thereof, to create the desired UI. It's these layouts that position and size their children. Typically you would use the subclasses ofPane
or use aGroup
, both of which inherit fromParent
. See Working with Layouts in JavaFX and thejavafx.scene.layout
package for more information. – Slaw