Use the setAlignment Method in JavaFX

MD Aminul Islam Jun 02, 2022
Use the setAlignment Method in JavaFX

HBox is a layout component that positions all of the components. By default, it will organize all the components in a horizontal row, but sometimes we may need to align it left, right, center, top, button, etc.

In this article, we will see how we can align the HBox in our own format. We will see an example and explain it line by line to make it easier to understand.

Use the setAlignment() Method in JavaFX

The libraries we are going to use in our example are the following:

import javafx.application.Application; // Package for the whole application
import javafx.geometry.Pos; // Geometric positioning Package for the variable "Pos"
import javafx.stage.Stage; // Package for the stage
import javafx.scene.Scene; // Package for the scene
import javafx.scene.layout.HBox; // The HBox Package
import javafx.scene.control.Button; // Package for the button

These are the necessary libraries that we must include, and the purpose was commented in the code. Now, we are going to describe the whole code.

Our main code will look like the below, and the purpose of each line is commented beside it:

public class FxHBox extends Application  {  //Our main class extends to the Application class
    Button buttonOne, buttonTwo;  // Declared two buttons
    HBox _hbox; // Declared a HBox
    Scene scene; // Declared a scene

    public void start(Stage BasicStage) throws Exception {  // We have to use Exception Handling here as it can generate errors if anything goes wrong.
        BasicStage.setTitle("HBox Set Allignment"); // Set the  title of the application
        buttonOne = new Button("Button 1"); // Created button 1 with title
        buttonTwo = new Button("Button 2"); // Created button 2 with title

        _hbox = new HBox(buttonOne, buttonTwo); // Created an HBox element that holds the two buttons buttonOne & buttonTwo
        _hbox.setAlignment(Pos.CENTER); // Here we are customizing the default alignment and place our componet in center. Here 'Pos' provides the geometic allignment.

        scene = new Scene(_hbox, 400, 300); // Creates the scene with necessary heights and weights
        BasicStage.setScene(scene); // Set the scene
        BasicStage.show(); // Visualize the scene
    }

    public static void main(String[] args) {
        Application.launch(args); // Launch the application
    }
}

The main point you have to notice here is how the setAlignment() method was used. Through this, we can customize the position of the Hbox.

There are various alignments that we can provide to our elements. These are listed below:

Positions Description
Pos.BASELINE_LEFT Vertically align Baseline Horizontally align Left
Pos.BASELINE_CENTER Vertically align Baseline Horizontally align Center
Pos.BASELINE_RIGHT Vertically align Baseline Horizontally align Right
Pos.BOTTOM_LEFT Vertically align Bottom Horizontally align Left
Pos.BOTTOM_CENTER Vertically align Bottom Horizontally align Center
Pos.BOTTOM_RIGHT Vertically align Bottom Horizontally align Right
Pos.CENTER_LEFT Vertically align Center Horizontally align Left
Pos.CENTER Vertically align Center Horizontally align Center
Pos.CENTER_RIGHT Vertically align Center Horizontally align Right
Pos.TOP_LEFT Vertically align Top Horizontally align Left
Pos.TOP_CENTER Vertically align Top Horizontally align Center
Pos.TOP_RIGHT Vertically align Top Horizontally align Right

Now let’s see the total view of our example code and its output. You can copy and paste the code below to have a test run.

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

public class FxHBox extends Application  {
    Button buttonOne, buttonTwo;
    HBox _hbox;
    Scene scene;

    public void start(Stage BasicStage) throws Exception {
        BasicStage.setTitle("HBox Set Allignment");
        buttonOne = new Button("Button 1");
        buttonTwo = new Button("Button 2");

        _hbox = new HBox(buttonOne, buttonTwo);
        _hbox.setAlignment(Pos.CENTER);

        scene = new Scene(_hbox, 400, 300);
        BasicStage.setScene(scene);
        BasicStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

After a successful compilation and run, you will get an output like the below:

setAlignment JavaFX Output

Remember, if your IDE doesn’t support the automatic inclusion of Libraries, then you may need to include necessary Library files manually before compiling.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Java JavaFX