Logo

dev-resources.site

for different kinds of informations.

GUI Design with JavaFX Layout Managers

Published at
1/13/2025
Categories
java
javafx
coding
programming
Author
alex_ricciardi
Categories
4 categories in total
java
open
javafx
open
coding
open
programming
open
Author
14 person written this
alex_ricciardi
open
GUI Design with JavaFX Layout Managers

This article explores how Java Layout Managers provide an abstraction that streamlines the development of Graphical User Interfaces (GUIs) in JavaFX by automating component sizing and positioning. Using predefined layouts like HBox, VBox, and GridPane, developers can create organized and responsive interfaces.


The Java Layout Manager provides an easy way to develop Graphical User Interfaces (GUIs), particularly by offering tools to manage and organize GUI components. It is responsible for determining the dimensions and placement of components within a container (Oracle Docs. n.d.). While components can suggest their preferred sizes and alignments, the layout manager of the container ultimately decides the final size and position of these components. The Java Layout Manager provides a simpler approach to using panes (Gordon, 2013). It also facilitates the creation and management of standard layouts like rows, columns, stacks, tiles, and more. Additionally, when the window is resized, the layout pane automatically adjusts the positioning and size of its contained nodes based on their properties, it is responsive. Additionally, this article offers a Zoo Exabit program example of how layout managers can be used to arrange UI elements.

JavaFX offers a variety of layouts that can fit different GUI needs and functionalities. Layouts such as HBox, VBox, GridPane, BorderPane, StackPane, and FlowPane, see Figure 1.

Figure 1
Example of JavaFX Layouts
Example of JavaFX Layouts
Note: from “3/10 — Introduction and overview of JavaFX panes or GUI containers for layout” by JavaHandsOnTeaching (2021)

Zoo Exabit Program Example

The program displays the animals found in various exhibits of a Zoo using the JavaFx VBox and HBox layouts, see Figure 2 to see how the different layout panes are positioned.

Figure 2
Zoo Layout Panes
Zoo Layout Panes

Java File Source Code:

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

public class Main extends Application {
 @Override
 public void start(Stage primaryStage) {

  // Title for the window
  Label title = new Label("Zoo Exhibits");
  title.getStyleClass().add("title");

  // Create main VBox layout
  VBox mainLayout = new VBox(20);
  // Center align the contents of the VBox (title and Horizontal boxes for the two
  // sets of exhibits)
  mainLayout.setAlignment(Pos.CENTER);

  // Horizontal boxes for the two sets of exhibits
  HBox firstSetExhibits = new HBox(10);
  firstSetExhibits.setAlignment(Pos.CENTER); // Center align the contents of the HBox
  firstSetExhibits.getChildren().add(createExhibitSection("Africa", "Lion", "Elephant", "Giraffe"));
  firstSetExhibits.getChildren().add(createExhibitSection("South America", "Jaguar", "Llama", "Macaw"));
  firstSetExhibits.getChildren().add(createExhibitSection("Australia", "Kangaroo", "Koala", "Platypus"));

  HBox secondSetExhibits = new HBox(10);
  secondSetExhibits.setAlignment(Pos.CENTER); // Center align the contents of the Exhibit HBox
  secondSetExhibits.getChildren()
    .add(createExhibitSection("North America", "Bison", "Bald Eagle", "Grizzly Bear"));
  secondSetExhibits.getChildren().add(createExhibitSection("Asia", "Tiger", "Panda", "Orangutan"));
  secondSetExhibits.getChildren().add(createExhibitSection("Europe", "Wolf", "Brown Bear", "Red Deer"));

  // Add the title and horizontal sets to the main layout
  mainLayout.getChildren().addAll(title, firstSetExhibits, secondSetExhibits);

  // Create a Scene
  Scene scene = new Scene(mainLayout, 500, 500);

  // Load the CSS file
  scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

  // Set the scene on the primary stage
  primaryStage.setTitle("Zoo Exhibits");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

 // String... passes multiple string an array of strings not a set size
 private VBox createExhibitSection(String continent, String... animals) { 
  VBox exhibitSection = new VBox(5);
  exhibitSection.setAlignment(Pos.CENTER); // Center align the exhibit section labels
  exhibitSection.getStyleClass().add("exhibit-section");

  // Title label for the continent
  Label continentLabel = new Label(continent);
  continentLabel.getStyleClass().add("continent-label");
  exhibitSection.getChildren().add(continentLabel);

  // Vertical box to hold animal labels
  VBox animalsBox = new VBox(5);
  animalsBox.setAlignment(Pos.CENTER); // Center align the animal labels
  for (String animal : animals) {
   Label animalLabel = new Label(animal);
   animalLabel.getStyleClass().add("animal-label");
   animalsBox.getChildren().add(animalLabel);
  }

  // Add the VBox to the section
  exhibitSection.getChildren().add(animalsBox);

  return exhibitSection;
 }

 public static void main(String[] args) {
  launch(args);
 }
}
Enter fullscreen mode Exit fullscreen mode

CSS file:

.title {
     -fx-font-size: 24px;
     -fx-font-weight: bold;
     -fx-padding: 10px;
     -fx-background-color: #f0f0f0;
}

.main-content-title {
     -fx-font-size: 20px;
     -fx-font-weight: bold;

     -fx-padding: 5px;
     -fx-background-color: #d0d0d0;
}

.exhibit-section {
     -fx-padding: 10px;
     -fx-background-color: #f9f9f9;
     -fx-border-radius: 5px;
}

.continent-label {
     -fx-font-size: 16px;
     -fx-font-weight: bold;
}

.animal-label {
     -fx-font-size: 14px;   
     -fx-border-radius: 3px;
     -fx-padding: 3px;

}
Enter fullscreen mode Exit fullscreen mode

Output:
Zoo Pane Output

To summarize, Java Layout Managers provided an easy way to develop GUI by offering a robust and flexible framework. Java Layout Managers’ abstraction allows developers to focus on creating organized layouts using predefined panes like HBox, VBox, GridPane, and others without having to manually manage the placement and sizing of each component. The Zoo Exhibits program shows how layout managers can be used to arrange UI elements and ensure that applications remain adaptable to window resizing and different display environments. Java Layout Managers is a powerful abstraction that not only facilitates the development process but also enhances the user experience by providing consistent and dynamic interface structures.


References:

Gordon, J. (2013, June). JavaFX: Working with layouts in JavaFX [PDF]. Oracle Docs. https://docs.oracle.com/javafx/2/layout/jfxpub-layout.pdf/

JavaHandsOnTeaching (2021, June 19). 3/10 — Introduction and overview of JavaFX panes or GUI containers for layout [Video]. YouTube. https://www.youtube.com/watch?v=GH-3YRAuHs0&t=905s

Oracle Docs. (n.d)._ Using layout managers_. The Java™ Tutorials. Oracle. https://docs.oracle.com/javase%2Ftutorial%2Fuiswing%2F%2F/layout/using.html


Originally published at Alex.omegapy on Medium, November 8, 2024.

java Article's
30 articles in total
Favicon
Wedensday Links - Edition 2025-01-15
Favicon
🚀 New Book Release: "Navigate the Automation Seas" – A Practical Guide to Building Automation Frameworks
Favicon
How to Manage Secrets Effectively Across Multiple Platforms with a Single Tool?
Favicon
Top 5 Backend Programming Languages to Learn in 2024
Favicon
Understanding Node.js Cluster: The Core Concepts
Favicon
Launched a Web version of my Project using Java Spring Framework, Spring Boot Web
Favicon
Developing a project using Java Spring Framework, JSON, JPA and PostgreSQL
Favicon
Designing an Internet Credit Purchase System
Favicon
5 Proven Strategies for Java Persistence Optimization
Favicon
Starting testing
Favicon
JSON Diff: Comparing and Identifying Changes in JSON Data
Favicon
How to index in SolrJ
Favicon
5 Advanced Java Multithreading Techniques for High-Performance Applications
Favicon
Java interacting with Apache Avro
Favicon
Default Methods in Java
Favicon
Java-kafka producing a message
Favicon
Interfaces funcionais predefinidas
Favicon
ReferĂŞncias de construtor
Favicon
Referências de método
Favicon
Pergunte ao especialista - referência a um método genérico
Favicon
Building and Deploying Your First Java App with Docker in Just 5 Minutes
Favicon
Token Bucket Rate Limiter (Redis & Java)
Favicon
Converting JPA entities to Mendix
Favicon
Understanding Spring Security and OAuth 2.0
Favicon
Secrets of Java's String Pool
Favicon
GUI Design with JavaFX Layout Managers
Favicon
Spring Oauth2 - App-Token based Hybrid Token Verification Methods
Favicon
My 2025 Dev Plan
Favicon
Mastering Java: A Beginner's Guide to Building Robust Applications
Favicon
Introduzione alla Programmazione in Java: Guida per Principianti | Introduction to Java Programming: A Beginner's Guide

Featured ones: