1
votes

I need some help getting components scaled on my scene graph. In the following image I have the components on the top, well spaced and everything looks nice and normal.

enter image description here

I did that by setting the padding on the top menu with

menu.setPadding(new Insets(30, 0, 30, 30));

and HBox.setMargin(button, new Insets(0,0,0,370)); for the button where 370 is the padding between the field and the button. Also, the text field has a preferred width of 400.

However, when I expand the window I realize the preferred width I set for the text field does not change and it is small, and the margins of the button also change, so I don't know how to set this numbers proportional to the size of the window. I am not sure if I fix this with CSS or simply java methods.

enter image description here

This is the code that deals with the top part where components have the issue when maximizing the window. 'pane' is an instance of BorderPane.

private void setTopMenu() {

 menu = new HBox();
 menu.setPadding(new Insets(30, 0, 30, 30));

 button = new Button("ABCD ABCD ABCD"); 

 tf = new TextField();
 tf.setPromptText("search"); 
 tf.setPrefWidth(400);
 tf.setId("text-field");

 top.getChildren().addAll(tf, button);

 HBox.setMargin(button, new Insets(0,0,0,370));

 pane.setTop(menu);
}
1
If you just need to resize the components inside the TopMenu, can't you bind the window's width to the width of your Button and TextArea's width? - Marco Luzzara
but how? @MarcoLuzzara - chris

1 Answers

1
votes

You have 2 ways:

  • the first one is the easiest but also the less flexible:

    HBox.setHgrow(button, Priority.ALWAYS);
    HBox.setHgrow(tf, Priority.ALWAYS);
    

    Specifying the Priority you can decide how the additional space between the components and the HBox will be filled.

  • Using the Property binding:

    button.prefWidthProperty().bind(menu.widthProperty().divide(3));
    

    In this example your button will be resized to 1/3 of the menu's WidthProperty everytime this last property changes. Here is better explained.