Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

FlowPane in JavaFX with Code | 100% Perfect for beginners

May 8, 2022
in Java
Reading Time: 5 mins read
0
Layout in JavaFX
92
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • How to use the FlowPane in JavaFX
    • Creating the JavaFX FlowPane
    • Alignment Property
      • Output
    • JavaFX FlowPane rowValignment and columnHalignment Properties
      • Output
    • hgap and vgap Properties
    • Orientation Property
    • prefWrapLength Property
    • YouTube Video

How to use the FlowPane in JavaFX

FlowPane in JavaFX is a very simple layout pane that lays out its children in rows and columns. The JavaFX FlowPane is something like a combination of HBox and VBox, so if you have a FlowPane layout in your application. “Flow Pane,” the name itself, lets the nodes flow horizontally or vertically. JavaFX FlowPane is used in situations where the content is necessary to display.

For example, you have a series of buttons horizontally or vertically. When the user resizes the window that doesn’t have a FlowPane layout, if the window is too small, the contents will not be displayed. Using FlowPane helps the layout to display the content as often as possible. If the window becomes smaller and smaller, the children inside the JavaFX FlowPane will flow horizontally or vertically. FlowPane in JavaFX may be arranged left to right or right to left, controlled by the nodeOrientation property.

Creating the JavaFX FlowPane

Creating the FlowPane in JavaFX is very easy. FlowPane provides several ways to make the FlowPane layout. You can create an empty FlowPane. You can also specify the orientation, spacing, and adding and initial nodes in the FlowPane. Now, let’s go over the example code given below and learn how to make the FlowPane layout in JavaFX.

// Create an empty FlowPane
FlowPane flowPane1 = new FlowPane();
        
// Create an empty FlowPane and set the orientation
FlowPane flowPane2 = new FlowPane(Orientation.VERTICAL);
        
// Create an empty FlowPane with 5px horizontal and 10px vertical spacing
FlowPane flowPane3 = new FlowPane(5, 10);
        
// Create an empty FlowPane with orientation and spacing
FlowPane flowPane4 = new FlowPane(Orientation.VERTICAL, 5, 10);
        
// Create a FlowPane with initial nodes inside
FlowPane flowPane5 = new FlowPane(new Button("Button 1"), new Button("Button 2"));

FlowPane has properties you need to learn. These properties are helpful when you are using the FlowPane Layout. Take time to read the descriptions of each property. These are the alignment, rowValignment, columnHalignment, hgap, vgap, orientation, prefWrapLength. Please proceed below.

  1. alignment – The alignment property is to align the FlowPane, and the default value is Pos.TOP_LEFT.
  2. rowValignment – This property specifies the vertical alignment of the nodes within each row in a horizontal FlowPane.
  3. columnHalignment – This property specifies the horizontal alignment of the nodes within each column in a vertical FlowPane.
  4. hgap and vgap – The hgap and vgap properties specify the nodes’ horizontal and vertical gaps.
  5. orientation – The orientation property specifies the orientation of the FlowPane.
  6. prefWrapLength – The preferred width in a horizontal FlowPane and the preferred height in a vertical FlowPane where the content should wrap and the default value is 400.

Alignment Property

The alignment property of the JavaFX FlowPane controls the alignment of its content. You need to set the parameter of the setAlignment() method, and the default value is Pos.TOP_LEFT. The Pos value contains a vertical alignment and horizontal alignment. In a horizontal FlowPane, each row is aligned using the hpos value of the alignment, and rows (the entire content) are aligned using the vpos value. In a vertical FlowPane, each column is aligned using the vpos value of the alignment, and the columns (the entire content) are aligned using the hpos value. Proceed to the example given below.

// Create an empty FlowPane
FlowPane root = new FlowPane();
        
// Setting the alignment to center right
root.setAlignment(Pos.CENTER_RIGHT);

Button b1 = new Button("One");
Button b2 = new Button("Two");
Button b3 = new Button("Three");

root.getChildren().addAll(b1, b2, b3);

Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Understanding FlowPane");
stage.show();

Output

FlowPane in JavaFX alignment

JavaFX FlowPane rowValignment and columnHalignment Properties

JavaFX FlowPane layouts its children at their preferred sizes. Rows and Columns could be of different sizes, but you can align the nodes or children in each row or column using the rowValignment and columnHalignment properties.

In a horizontal JavaFX FlowPane, the nodes in one row may be of different heights. The row’s height is the largest of the preferred heights of all nodes in the row. The rowValignment property lets you specify the vertical alignment of the nodes in each row. While in a vertical JavaFX FlowPane, the nodes in one column may be of different widths. The column’s width is the largest of the preferred widths of all nodes in the column. The columnHalignment property lets you specify the horizontal alignment of children in each column. Proceed to the example given below.

public void start(Stage stage) {
     FlowPane f1 = createFlowPane(HORIZONTAL, VPos.TOP, HPos.LEFT);
     FlowPane f2 = createFlowPane(HORIZONTAL, VPos.CENTER, HPos.LEFT);
     FlowPane f3 = createFlowPane(VERTICAL, VPos.CENTER, HPos.RIGHT);
     
     HBox root = new HBox(f1, f2, f3);

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setTitle("Understanding FlowPane");
    stage.show();
}

private FlowPane createFlowPane(Orientation orientation, VPos rowAlign, HPos colAlign){
    
    Text text = new Text();
    if(orientation == Orientation.HORIZONTAL){
        text.setText(rowAlign.toString());
    }else{
        text.setText(colAlign.toString());
    }
    
    TextArea textArea = new TextArea(orientation.toString());
    textArea.setPrefColumnCount(5);
    textArea.setPrefRowCount(3);
    
    FlowPane flowPane = new FlowPane(orientation, 5, 10);
    flowPane.setRowValignment(rowAlign);
    flowPane.setColumnHalignment(colAlign);
    flowPane.setPrefSize(175, 130);
    flowPane.getChildren().addAll(text, textArea);
    flowPane.setStyle("-fx-padding: 10; -fx-border-style: solid inside; -fx-border-width: 2; -fx-border-insets: 5; -fx-border-radius: 5; -fx-border-color: red;");
    
    return flowPane;
}

Output

JavaFX FlowPane alignment

hgap and vgap Properties

The hgap and vgap in the JavaFX FlowPane are very easy to use. Using these properties will help you set gaps in between the nodes. Using the hgap property, the horizontal spacing between the nodes of children in a row. The vgap property specifies the spacing between the nodes or children in a column. Proceed to the example given below to learn how the hgap and vgap work.

// create a FlowPane with 5px hgap and 10px vgap
FlowPane flowPane = new FlowPane(5, 10);

// or setting the gap using the method
flowPane.setHgap(15);
flowPane.setVgap(10);

Orientation Property

The orientation property lets you set the orientation of FlowPane. To set the orientation of the FlowPane, you can set it to Orientation.HORIZONTAL. You can also set the orientation using the setter method. Proceed to the example given below.

// create a horizontal FlowPane
FlowPane flowPane = new FlowPane(Orientation.HORIZONTAL);

// setting the FlowPane's orientation using the setter method
flowPane.setOrientation(Orientation.HORIZONTAL);

prefWrapLength Property

The prefWrapLength property is the preferred width in a horizontal FlowPane or the preferred height in a vertical FlowPane where content should wrap. The prefWrapLength property is used to compute the preferred size of the FlowPane. Treat the value of this property as a hint to resize your FlowPane.

YouTube Video

YouTube video
Previous Post

JavaFX VBox Tutorial With Code | 100% Perfect Tutorial

Next Post

BorderPane JavaFX Tutorial with Code | 100% Perfect tutorial

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
587
Next Post
Layout in JavaFX

BorderPane JavaFX Tutorial with Code | 100% Perfect tutorial

Layout in JavaFX

StackPane JavaFX Tutorial | 100% Perfect for beginners

JavaFX Button

JavaFX Button with examples | 100% Perfect for beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.