Search code examples
javafxvbox

How to align controls from to differents VBox in JAVAFX


I have a Vbox with 5 Text classes, and other Vbox with a TextField, DatePicker, RadioButton, ToggleButton and a check box. 5 and 5. I want to align the first Text class in the first Vbox with the first control in the second Vbox, using a HBox, successively. How could i do?

VBox verticaltextbox1 = new VBox();

    Text nombre = new Text("Nombre");
    Text fecha = new Text("Fecha");
    Text genero = new Text("Genero");
    Text reservacion = new Text("Reservacion");
    Text tecnologias = new Text("Tecnologias conocidas");

VBox verticaltextbox2 = new VBox();

    TextField caja = new TextField();
    DatePicker calendario = new DatePicker(LocalDate.now());
    RadioButton masculino = new RadioButton("Masculino");
    ToggleButton reservacionSi = new ToggleButton("Si");
    CheckBox java = new CheckBox("Java");

verticaltextbox1.getChildren().addAll(nombre, fecha, genero, 
reservacion,tecnologias);

verticaltextbox2.getChildren().addAll(caja, calendario, masculino, 
reservacionSi, java);

HBox horizontalbox = new HBox(50);
horizontalbox.getChildren().addAll(verticaltextbox1,verticaltextbox2)

Solution

  • You're probably better of using GridPane:

    GridPane gridPane = new GridPane();
    gridPane.setHgap(50);
    
    gridPane.addRow(0, text1, textField);
    gridPane.addRow(1, text2, datePicker);
    gridPane.addRow(2, text3, radioButton);
    gridPane.addRow(3, text4, toggleButton);
    gridPane.addRow(4, text5, checkBox);
    

    This aligns the text nodes horizontally and the rest of the nodes horizontally in columns and aligns corresponding text and control nodes vertically in a grid.