×

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All methods needed for this purpose are present in the javafx.scene.layout.HBox class.

The constructor of HBox layout:

1. HBox():

It is used to create an empty HBox with zero spacing.

2. HBox(Double sp):

It is used to create HBox with specified spacing.

Methods to set HBox as a layout:

1. setAlignment(Double v)

This property of the HBox class allows us to set the alignment of the layout.

2. setFillHeight(Double v)

This property of HBox class is used to set height of the node as height of the HBox.

3. setSpacing(Double v)

This property of the HBox class is used to set space between two nodes in the HBox.

Layouts in JavaFX applications- Simple HBox

Example:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class HBoxUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      Button button1 = new Button(" Submit  ");
          Button button2 = new Button(" Clear ");
         
        HBox box=new HBox();
        


    Scene scene = new Scene(box,600,400);  
    box.getChildren().addAll(button1,button2);
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX Layout - HBox Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the HBox in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Button,  javafx.scene.layout.HBox.

Then we have created one class named HBoxUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold HBox, a HBox object is created which is then passed to the Scene class object.

HBox is created with the help of its constructor and components are added using addAll() method.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Layout – HBox Example”. Also, it displays HBox with two buttons Submit and Clear.

Layouts in JavaFX applications- HBox

Layouts in JavaFX applications- HBox using spacing:

Example:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      Button button1 = new Button(" Submit  ");
          Button button2 = new Button(" Clear ");
         
        HBox box=new HBox();
        


    Scene scene = new Scene(box,600,400);  
    box.getChildren().addAll(button1,button2);
         box.setSpacing(50);;
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX Layout - HBox Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the HBox with spacing in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Button,  javafx.scene.layout.HBox.

Then we have created one class named HBoxUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold HBox with space between nodes, a HBox object is created which is then passed to the Scene class object.

HBox is created with the help of its constructor and components are added using addAll() method and space is mentioned using setSpacing() method on HBox object.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Layout – HBox Example”. Also, it displays HBox with two buttons Submit and Clear with mentioned space in it.

Layouts in JavaFX applications- HBox

Related Topics

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

4 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.