🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to use the JavaFX HBox layout in the JavaFX application.
JavaFX HBox
The JavaFX HBox component is a layout component that positions all its child nodes (components) in a horizontal row.
The HBox layout is represented by javafx.scene.layout.HBox class. We just need to instantiate the HBox class in order to create an HBox layout.
Complete JavaFX tutorial at https://www.javaguides.net/p/javafx-tutorial.html.
JavaFX HBox Example
The example shows six buttons in a single row. The row is right-aligned. There is some space between the buttons:
package com.javafx.examples.layout;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class HBoxExample extends Application {
@Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
HBox root = new HBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.BASELINE_RIGHT);
Button prevBtn = new Button("Previous");
Button nextBtn = new Button("Next");
Button cancBtn = new Button("Cancel");
Button helpBtn = new Button("Help");
Button exitBtn = new Button("Exit");
Button stopBtn = new Button("Stop");
root.getChildren().addAll(prevBtn, nextBtn, cancBtn, helpBtn, exitBtn, stopBtn);
Scene scene = new Scene(root);
stage.setTitle("Row of buttons");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}Let's understand the above JavaFX program.
A HBox pane is created with some spacing:
HBox root = new HBox(5);We create some padding around the HBox:
root.setPadding(new Insets(10));The setAlignment() method aligns the nodes to the right:
root.setAlignment(Pos.BASELINE_RIGHT);The buttons are added to the container:
root.getChildren().addAll(prevBtn, nextBtn, cancBtn, helpBtn, exitBtn, stopBtn);We created six buttons:
Button prevBtn = new Button("Previous");
Button nextBtn = new Button("Next");
Button cancBtn = new Button("Cancel");
Button helpBtn = new Button("Help");
Button exitBtn = new Button("Exit");
Button stopBtn = new Button("Stop");Output
Related JavaFX Examples
- JavaFX Hello World Example Tutorial - In this tutorial, we will learn how to create our first JavaFX application.
- Registration Form Using JavaFX with MySQL Database - In this tutorial, we will learn how to create a Registration Form using JavaFX with database connectivity. Here we will use the MySQL database to store user data via JDBC API.
- Login Form Using JavaFX with MySQL Database - In this tutorial, we will learn how to create a Login Form using JavaFX with database connectivity.
- JavaFX Quit Button Example - Terminate Application - In this tutorial, we will learn how to stop or terminate the JavaFX application.
- JavaFX Text, Font and Color Example Tutorial - In this tutorial, we will learn how to create text, adding font to text, adding color to text in the JavaFX application.
- JavaFx BorderPane Example Tutorial - In this tutorial, we will learn how to use the BorderPane layout in the JavaFX application.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business

Comments
Post a Comment
Leave Comment