HBox is a part of JavaFX. HBox lays out its children in form of horizontal columns. If the HBox has a border and/or padding set, then the contents will be layed out within those insets. HBox class extends Pane class.
Constructors of the class:
- HBox(): Creates an HBox object with no nodes.
 - HBox(double s): Creates an HBox with spacing in between nodes.
 
Commonly Used Methods:
| Method | Explanation | 
|---|---|
| getAlignment() | Returns the value of property alignment. | 
| getSpacing() | Returns the spacing between its children. | 
| setAlignment(Pos value) | Sets the Alignment of the HBox. | 
| getChildren() | Returns the nodes in HBox. | 
Below programs illustrate the use of HBox class:
- Java Program to create a HBox and add it to the stage: In this program we will create a HBox named hbox. Now create a label and add it to the hbox. We will also create some buttons and add them to the HBox using the getChildren().add() function. Now create a scene and add the hbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a HBox// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;ÂÂpublicclassHBOX_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("HBox");           Â// create a HBox           ÂHBox hbox =newHBox();           Â// create a label           ÂLabel label =newLabel("this is HBox example");           Â// add label to hbox           Âhbox.getChildren().add(label);           Â// add buttons to HBox           Âfor(inti =0; i <10; i++)           Â{               Âhbox.getChildren().add(newButton("Button "                                          Â+ (int)(i +1)));           Â}           Â// create a scene           ÂScene scene =newScene(hbox,800,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
 - Java Program to create a HBox, add spaces between its elements and add it to the stage: In this program we will create a HBox named hbox. Set the spacing by passing a double value of space as an argument to the constructor. Now create a label and add it to the hbox. To add some buttons to the HBox use the getChildren().add() function. Finally, create a scene and add the hbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a HBox, add// spaces between its elements and add// it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;ÂÂpublicclassHBOX_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("HBox");           Â// create a HBox           ÂHBox hbox =newHBox(10);           Â// create a label           ÂLabel label =newLabel("this is HBox example");           Â// add label to hbox           Âhbox.getChildren().add(label);           Â// add buttons to HBox           Âfor(inti =0; i <5; i++)           Â{               Âhbox.getChildren().add(newButton("Button "                                         Â+ (int)(i +1)));           Â}           Â// create a scene           ÂScene scene =newScene(hbox,800,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
 - Java Program to create a HBox, add spaces between its elements, set an alignment and add it to the stage: In this program we will create a HBox named 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().add() function. Finally, create a scene and add the hbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a HBox, add spaces// between its elements, set an alignment// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;importjavafx.geometry.*;ÂÂpublicclassHBOX_3extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("HBox");           Â// create a HBox           ÂHBox hbox =newHBox(10);           Â// setAlignment           Âhbox.setAlignment(Pos.CENTER);           Â// create a label           ÂLabel label =newLabel("this is HBox example");           Â// add label to hbox           Âhbox.getChildren().add(label);           Â// add buttons to HBox           Âfor(inti =0; i <5; i++)           Â{               Âhbox.getChildren().add(newButton("Button "                                         Â+ (int)(i +1)));           Â}           Â// create a scene           ÂScene scene =newScene(hbox,800,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
 
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/layout/HBox.html

                                    