Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align children in a HBox Left, Center and Right

Tags:

java

javafx

I have a HBox with children set up as follows:

HBox h = new HBox();
h.setMinWidth(555);

Label leftLabel = new Label("Left");
Label centerLabel = new Label("Center");

HBox rightContent = new HBox();
Label r1 = new Label("2");
Label r2 = new Label("3");

rightContent.getChildren().addAll(r1, r2);
h.getChildren().addAll(leftLabel, centerLabel, rightContent);

This will create a HBox with all the children floated on the left. I would like leftLabel to be on the left, centerLabel at the center, and rightContent to the extreme right.

How can I achieve this?

Thank you all in advance.

like image 412
Program-Me-Rev Avatar asked Jan 14 '17 19:01

Program-Me-Rev


People also ask

How to align HBox?

Set the spacing by passing a double value of space as an argument to the constructor. Set the alignment of the HBox using the setAlignment() function. Then create a label and add it to the hbox. Add some buttons to the HBox using the getChildren().

How to set position of HBox JavaFX?

You can set the margin for child nodes of a JavaFX HBox using the static setMargin() method. Here is an example of setting the margin around a JavaFX Button using the setMargin() method: Button button1 = new Button("Button 1"); HBox hbox = new HBox(button1); HBox. setMargin(button1, new Insets(10, 10, 10, 10));

What is region JavaFX?

Region is the base class for all JavaFX Node-based UI Controls, and all layout containers. It is a resizable Parent node which can be styled from CSS. It can have multiple backgrounds and borders. It is designed to support as much of the CSS3 specification for backgrounds and borders as is relevant to JavaFX.


1 Answers

You can use the following trick:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.stage.Stage;


public class Main23 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label1 = new Label("Left");
        label1.setStyle("-fx-background-color: red; -fx-text-fill: white;");

        Label label2 = new Label("Center");
        label2.setStyle("-fx-background-color: green; -fx-text-fill: white;");

        Label label3 = new Label("Right");
        label3.setStyle("-fx-background-color: blue; -fx-text-fill: white;");

        Region region1 = new Region();
        HBox.setHgrow(region1, Priority.ALWAYS);

        Region region2 = new Region();
        HBox.setHgrow(region2, Priority.ALWAYS);

        HBox hBox = new HBox(label1, region1, label2, region2, label3);

        primaryStage.setScene(new Scene(hBox, 640, 240));
        primaryStage.show();
    }
}

As you can see I just add several Region controls for fill space between labels and set Hgrow property to ALWAYS.

enter image description here

This trick is appropriate in some cases because sometimes you can't use other layouts i.e. if you want to align buttons inside of ToolBar.

like image 102
Maxim Avatar answered Nov 15 '22 17:11

Maxim