The Wayback Machine - https://web.archive.org/web/20240717024653/https://www.geeksforgeeks.org/javafx-hbox-class/
Open In App

JavaFX | HBox Class

Last Updated : 07 Sep, 2018
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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:

  1. 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 stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.Group;
      
    public class HBOX_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("HBox");
      
                // create a HBox
                HBox hbox = new HBox();
      
                // create a label
                Label label = new Label("this is HBox example");
      
                // add label to hbox
                hbox.getChildren().add(label);
      
                // add buttons to HBox
                for (int i = 0; i < 10; i++) 
                {
                    hbox.getChildren().add(new Button("Button " 
                                               + (int)(i + 1)));
                }
      
                // create a scene
                Scene scene = new Scene(hbox, 800, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  2. 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 stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.Group;
      
    public class HBOX_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("HBox");
      
                // create a HBox
                HBox hbox = new HBox(10);
      
                // create a label
                Label label = new Label("this is HBox example");
      
                // add label to hbox
                hbox.getChildren().add(label);
      
                // add buttons to HBox
                for (int i = 0; i < 5; i++) 
                {
                    hbox.getChildren().add(new Button("Button " 
                                              + (int)(i + 1)));
                }
      
                // create a scene
                Scene scene = new Scene(hbox, 800, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  3. 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 stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.Group;
    import javafx.geometry.*;
      
    public class HBOX_3 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("HBox");
      
                // create a HBox
                HBox hbox = new HBox(10);
      
                // setAlignment
                hbox.setAlignment(Pos.CENTER);
      
                // create a label
                Label label = new Label("this is HBox example");
      
                // add label to hbox
                hbox.getChildren().add(label);
      
                // add buttons to HBox
                for (int i = 0; i < 5; i++) 
                {
                    hbox.getChildren().add(new Button("Button " 
                                              + (int)(i + 1)));
                }
      
                // create a scene
                Scene scene = new Scene(hbox, 800, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(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



Similar Reads

JavaFX | AmbientLight Class
AmbientLight class is a part of JavaFX. This class defines an Ambient light object. AmbientLight class creates a light source that seems to come from all direction. The constructor of the class are: AmbientLight(): Creates a ambient light source with default white color AmbientLight(Color c): Creates a ambient light source with specified color The
4 min read
JavaFX | TextFlow Class
TextFlow class is a part of JavaFX. TextFlow class is designed to lay out rich text. It can be used to layout several Text nodes in a single text flow. TextFlow class extends Pane class. Constructors of the class: TextFlow(): Create a new textflow object. TextFlow(Node... c): Create a new textflow object with specified nodes. Commonly Used Methods:
4 min read
JavaFX | Tab Class
Tab class is a part of JavaFX. Tab class creates an object that is contained in TabPane. A Tab can contain any node as its content. A TabPane can contain multiple tabs. When the user clicks on the tab the content of the tab is made visible to the user. Constructors of the class: Tab(): Creates a new empty tab. Tab(String t): Creates a new tab with
4 min read
JavaFX | HTMLEditor Class
HTMLEditor class is a part of JavaFX. HTMLEditor allows the user to edit the existing HTML text and also apply styling to the text. The underlying data model is HTML but it is not visible to the user. Constructor of the class: HTMLEditor(): Creates a new object of HTMLEditor. Commonly Used Methods: Method Explanation getHtmlText() Returns the HTML
3 min read
JavaFX | Cursor class with examples
Cursor class is a part of JavaFX. Cursor class is used to encapsulate the bitmap representation of the mouse cursor. The cursor class has several predefined cursors that can be used according to the needs of the programmer. Commonly used Methods: Method Explanation cursor(String s) returns cursor object of the specified string toString() Returns a
4 min read
JavaFX | PieChart Class
PieChart class is a part of JavaFX. PieChart class is used to create a pie chart. The chart content is populated by pie slices based on data set on the PieChart.The layout of the pie chart is set to clockwise by default. PieChart inherits Chart class. Constructor of the class are: PieChart(): Creates an empty instance of pie chart.PieChart(Observab
4 min read
JavaFX | Shadow Class
Shadow class is a part of JavaFX. Shadow class creates a monochromatic shadow with blurry edges. The Shadow is of black Color (by default) and can be combined with the original to create a shadow. The Shadow of different color can be added with original to create a Glow effect. Shadow class inherits Effect class. Constructors of the class: Shadow()
4 min read
JavaFX | Insets Class
Insets class is a part of JavaFX. Insets class stores the inside offsets for the four sides of the rectangular area. Insets class inherits java.lang.Object class. Constructors of the class: Insets(double a): Constructs a new Insets instance with same value for all four offsets. Insets(double top, double right, double bottom, double left): Construct
4 min read
JavaFX | Point2D Class
Point2D class is a part of JavaFX. This class defines a 2-dimensional point in space. The Point2D class represents a 2D point by its x, y coordinates. It inherits java.lang.Object class.Constructor of the class: Point2D(double x, double y): Create a point2D object with specified x and y coordinates. Commonly Used Methods: MethodExplanationdistance(
4 min read
JavaFX | Canvas Class
Canvas class is a part of JavaFX. Canvas class basically creates an image that can be drawn on using a set of graphics commands provided by a GraphicsContext. Canvas has a specified height and width and all the drawing operations are clipped to the bounds of the canvas. Constructors of the class: Canvas(): Creates a new canvas object. Canvas(double
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg