Gaming News | Gaming Opinion | Playstation 4 | Xbox One | PS Vita | Film | Media

JavaFX – Borderpane and HBox layouts

The article will provide a user with information on JavaFX layouts. A JavaFX application can manually lay out the UI by setting the position and size properties for each UI element. The JavaFX SDK provides several layout container classes, called panes, for the easy setup and management of classic layouts such as rows, columns, stacks, tiles, and others.

The BorderPane layout pane provides five regions in which to place nodes: top, bottom, left, right, and center. When a user creates a layout with a border pane then there is a choice to have regions of any size. If the application does not need one of the regions, then a user does not need to define it. A border pane is useful for the classic look of a tool bar at the top, a status bar at the bottom, a navigation panel on the left, additional information on the right, and a working area in the center.

If a user wants to create a border pane with a coloured rectangle in each region then following example is best to follow:
BorderPane border = new BorderPane();
border.setTop(new Rectangle(200, 50, Color.DARKCYAN));
border.setBottom(new Rectangle(200, 50, Color.DARKCYAN));
border.setCenter(new Rectangle(100, 100, Color.MEDIUMAQUAMARINE));
border.setLeft(new Rectangle(50, 100, Color.DARKTURQUOISE));
border.setRight(new Rectangle(50, 100, Color.DARKTURQUOISE));
As the window is resized, the regions within the layout pane are resized according to the layout constraints assigned to the nodes within the pane.

The HBox layout pane provides an easy way for arranging a series of nodes in a single row.
The padding attributes can be set to manage the distance between the nodes and the edges of the HBox pane. Spacing attributes can be set to manage the distance between the nodes. The style can be set to change the background color.
An example will show how to create an HBox pane for a tool bar that contains two buttons.
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setStyle(“-fx-background-color: #336699″);

Button buttonCurrent = new Button(“Current”);
buttonCurrent.setMaxSize(100, 10);

Button buttonProjected = new Button(“Projected”);
buttonProjected.setMaxSize(100, 10);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);

As the window is resized, nodes within the HBox pane are resized according to the layout constraints assigned to the nodes.

If a user has read this article then he/she has learnt about the border pane and Hbox for the JavaFX.

Leave a Comment