Search code examples
javafxalignmenthbox

JavaFx Children alignment between 2 HBoxes


Trying to align children(labels) of 2 different HBoxes, which was created by the same method:

public HBox addHBox() {
    HBox hbox = new HBox(10);
    hbox.setLayoutX(10);
    hbox.setPrefWidth(Pane.getWidth());
    hbox.setPrefHeight(Pane.getHeight());
    return hbox;
}

The LayoutY is different, and changed after the creation of each HBox. Thus, I want to accompish that added child[i] of the HBox1, would be right below child[i] of the HBox2.

Note: the size of the children should not matter! (this is what bothers me)

I tried

hbox2.setMinWidth(hbox1.getMinWidth());

and

hbox2.setPrefWidth(hbox1.getPrefWidth());

and

hbox2.setMaxWidth(hbox1.getMaxWidth());

Nothing seems to help. Any ideas? P.S. I know that i can use gridPane, but it's not in this case


Solution

  • Your HBox only classifies your Nodes one side of the other horizontally, and in your case I suppose that you use a Text control to display your String, I don't say that it is a bad idea but I think that the width of your Node(Text) depends on the length of the string which gives an unpredictable and disorderly display, so why not use a Label, the advantage with this control is that you can define the Width of the background what Will give your String a limit and allow an ordered display !