JavaFX: Fit child elements to HBox width - javafx

Is it possible to manage child elements in a HBox, so that the sum of the widths of all child elements is equal to the width of the HBox?
So that elements fill the HBox and no space is left.
The height of the child elements is by default the height of the HBox, so how about the width? I don't want to calculate the width in my program. It would be better if the layout does this automatically, so that no calculation is needed.

It depends what kind of children does the HBox contain. Some of the children may not be resizable nodes. However, generally speaking, you can use HBox.setHgrow() method and set the same Priority for all children of hbox. The explanation is in its javadoc:
Sets the horizontal grow priority for the child when contained by an
hbox. If set, the hbox will use the priority to allocate additional
space if the hbox is resized larger than it's preferred width. If
multiple hbox children have the same horizontal grow priority, then
the extra space will be split evening between them. If no horizontal
grow priority is set on a child, the hbox will never allocate it
additional horizontal space if available. Setting the value to null
will remove the constraint.
Additionally, if you are trying to obtain a grid-like layout then try out other layout options, for instance TilePane or FlowPane and maybe GridPane.

The simple code for your answer would be this:
Button button1 = new Button("Save");
Button button2 = new Button("Delete");
HBox.setHgrow(button1, Priority.ALWAYS);
HBox.setHgrow(button2, Priority.ALWAYS);
It's recommended to use HBox class rather than name of HBox pane.

In FXML:
<HBox>
<Label HBox.hgrow="ALWAYS" maxWidth="Infinity">
TEST
</Label>
</HBox>

Adding to #bdshahab's answer, since HBox will try to resize their children to their preferred widths, they will only fill the box if their combined preferred widths are wider than the HBox width.
So it may be necessary to do something like:
Button button1 = new Button("Save");
Button button2 = new Button("Delete");
HBox.setHgrow(button1, Priority.ALWAYS);
HBox.setHgrow(button2, Priority.ALWAYS);
button1.setPrefWidth(100000);
button2.setPrefWidth(100000);

Related

Change Child node width in an HBOX

I am trying to change hbox cell width via changing child node width in the hbox
Hbox hbox = new Hbox(new Label("node1"), new Label(node2), new Label(node3));
//Lets say I wanna change the lable says "node3"
//i tried below option. did not work
hbox.getChildren().get(2).maxWidth(150);
node.maxWidth() is invoked by layouts to query the maximum size of the node, to change child width (label) cast it then use the setter
((Label) hBox.getChildren().get(2)).setPrefWidth(150);

Resizeable Gridpane or other container

Hi I'm trying to create a simple layout that looks like this using JavaFX.
I would like the user to be able to drag/resize the middle bar. I've tried to make this using a GridPane. But I can't figure out how to get the middle resized. Perhaps GridPane is not the way to go. Both panes will contain a lot of other program code later on. Thanks!
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stageRef.setMaxWidth(primaryScreenBounds.getWidth());
stageRef.setMaxHeight(primaryScreenBounds.getHeight());
GridPane gridPane = new GridPane();
gridPane.setGridLinesVisible(true);
gridPane.setPadding(new Insets(10));
Scene scene = new Scene(gridPane);
VBox vBoxMain = new VBox();
vBoxMain.setPrefWidth((primaryScreenBounds.getWidth()/5)*4);
vBoxMain.setPrefHeight(primaryScreenBounds.getHeight());
TextArea textArea = new TextArea();
textArea.setWrapText(true);
textArea.setPrefHeight(primaryScreenBounds.getHeight());
vBoxMain.getChildren().addAll(textArea);
vBoxMain.isResizable();
VBox vBoxSide = new VBox();
vBoxSide.setPrefWidth((primaryScreenBounds.getWidth()/5));
vBoxSide.setPrefHeight(primaryScreenBounds.getHeight());
vBoxSide.isResizable();
gridPane.add(vBoxSide, 1,1,1,1);
gridPane.add(vBoxMain, 2,1,4,1);
stageRef.setScene(scene);
stageRef.show();
You could use a SplitPane:
A control that has two or more sides, each separated by a divider,
which can be dragged by the user to give more space to one of the
sides, resulting in the other side shrinking by an equal amount.
Then you add two others containers to this pane allowing the user to change the position of the divider. You can also set minimum widths for each component within the pane or set the position of each divider within your code.

JavaFX buttons with same size

I have these buttons with different size:
Image
How I can make all buttons with same with size?
It depends on layout where the button is located. For example, if you add all the buttons into GridPane or BorderPane, you have to specify each button width to correspond to certain variable. In the following example I wrap all buttons inside VBox, set VBox preference width and tie up all buttons minimum width to it:
VBox vBox = new VBox();
vBox.setPrefWidth(100);
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMinWidth(vBox.getPrefWidth());
btn2.setMinWidth(vBox.getPrefWidth());
vBox.getChildren().addAll(btn1, btn2);
It is also worth to mention that there are two ways to specify the button size. You can do it in the java code or specify it in javafx .fxml file. The above method is an example for java code implementation.
You can also unclamp a button's maximum dimensions so it will grow to fill the available space (unlike most nodes, by default a button node has it's max size clamped to it's preferred size so it doesn't usually grow to fill available space). An Oracle tutorial on Tips for Sizing and Aligning Nodes explains this in more detail.
VBox vBox = new VBox();
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMaxWidth(Double.MAX_VALUE);
btn2.setMaxWidth(Double.MAX_VALUE);
vBox.getChildren().addAll(btn1, btn2);
using css you can override the preferred width of all buttons like
.button {
-fx-pref-width: 200px;
}
or create your own style class for certain button groups and add the style to the button like:
css:
.my-special-button {
-fx-pref-height: 28px;
-fx-pref-width: 200px;
}
and then set the style to your button with either
fxml:
styleClass="my-special-button"
or in java
myButton.getStyleClass().add("my-special-button");

How to set the width of a component(HBOX) dynamically in flex

I have a custom component that is extending the HBOX. Am adding a VBOX and a "HBOX" to it as its children.The VBOX has a label which acts as title to component and adding/deleting a pair of LINK BUTTONS to the "HBOX" dynamically on a particular event(might be a button click).
The parent HBOX width must grow/shrink whenever the link button is added/deleted dynamically.
How to calculate the same.I do not know.
you can set width and height dinamically with this:
var hb:HBox;
hb.percentHeight = 100; //height:100%
hb.percentWidth = 100; //widht:100%
You can set width with percent (100%) or bind width property to another width property (in mxml with {}).

Flex spacing inside vbox, hbox

children inside hbox and vbox have spacing between them,
how do you remove this empty space?
I need to have 0 space between child elements of a hbox or vbox
Set the verticalGap/horizontalGap properties to 0.
You should probably still set the horizontalGap to 0, and than add paddingLeft and paddingRight to all the elements that need spacing...

Resources