Table of contents
1.
Introduction
2.
JavaFX FlowPane
2.1.
Syntax
3.
Constructors 
4.
Methods of JavaFX FlowPane
5.
Examples of JavaFX FlowPane Implementation
5.1.
Java program demonstrating the Flow pane
5.2.
Java program demonstrating the Flow pane with only buttons
5.3.
Java program demonstrating the Flow pane using only buttons
6.
Frequently Asked Questions
6.1.
What are the various pane types in JavaFX?
6.2.
How are Flowpane and VBox different?
6.3.
In JavaFX, what is a node?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaFX FlowPane

Author Prerna Tiwari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaFX 3D shapes are geometrical figures that can be represented on the X, Y, and Z planes at the same time. The length or depth is represented by the X plane, the height by the Y plane, and the width by the Z plane. Cube, Cuboid, Cylinder, Cone, Sphere, and other general 3D shapes Shape3D class includes 3D shapes, and the Shape class includes all shapes (2D and 3D shapes). 

JavaFX FlowPane

FlowPane is a JavaFX class that arranges its child nodes along the flow pane's boundary. There are two types of flowpanes: horizontal and vertical. The former arranges the child nodes in rows that wrap around the pane width, while the latter arranges the child nodes in columns that wrap around the pane height. The class javafx.scene.layout.FlowPane can be used to create a Java FlowPane. 

Syntax

Let's look at the syntax of FlowPane now.

FlowPane fp = new FlowPane();

Constructors 

JavaFX FlowPane has several constructors. Depending on the situation, it can be parameterized or non-parameterized.

They are as follows:

Methods of JavaFX FlowPane

JavaFX FlowPane Methods Java FlowPane has various methods that implement various functionalities.

The most commonly used methods in FlowPane are.

Examples of JavaFX FlowPane Implementation

Let's look at some different ways to implement JavaFX FlowPane.

Java program demonstrating the Flow pane

import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.control.Label;

// sample class extends the application class

public
class JavaFXFlowPaneExample extends Application
{
    // application starts at this point
    @Override public void start(Stage s) throws Exception
    {
        // create label 1
        Label labl1 = new Label("Name of your institution");
        // create label 2
        Label labl2 = new Label("Experience : (In months) ");
        // create textfield 1
        TextField txt1 = new TextField();
        // create textfield 2
        TextField txt2 = new TextField();
        // create a button
        Button b = new Button("Click here to login ! !");
        // create Flowpane
        FlowPane fp = new FlowPane(30.0, 30.0, labl1, txt1, labl2, txt2, b);
        // create scene
        Scene sc = new Scene(fp, 900, 200);
        // set scene
        s.setScene(sc);
        // display result
        s.show();
    }
    // main method
public
    static void main(String[] args)
    {
        launch(args);
    }
}

 

Output


When the code is executed, two text fields with two labels and a button are displayed. These text fields and buttons are all located on the same row. It's because the flow pane's default alignment is horizontal. If the labels or buttons exceed the dimensions specified, the remainder will be placed in the next row.

Java program demonstrating the Flow pane with only buttons

import java.awt.Insets;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
// class that extends application class
public
class JavaFXFlowPaneExample extends Application
{
    // application starts at this point
    @Override public void start(Stage s) throws Exception
    {
        // set title
        s.setTitle("FlowPane Example");
        // create buttons
        Button bt1 = new Button("Fruits");
        Button bt2 = new Button("Vegetables");
        Button bt3 = new Button("Groceries");
        Button bt4 = new Button("Households");
        Button bt5 = new Button("Juices");
        Button bt6 = new Button("Diary products");
        // create Flow pane
        FlowPane fp = new FlowPane();
        // Set the horizontal gap
        fp.setHgap(25);
        // observable list
        ObservableList<Node> li = fp.getChildren();

        // Add all the nodes to the flow pane
        li.addAll(bt1, bt2, bt3, bt4, bt5, bt6);

        // scene
        Scene sc = new Scene(fp, 700, 100);
        // set scene
        s.setScene(sc);
        // display the result
        s.show();
    }
    // main method
public
    static void main(String[] args)
    {
        Application.launch(args);
    }
}

 

Output

A Flow pane is created by arranging six buttons in a single row. If more buttons are added, they will appear in subsequent rows.

Java program demonstrating the Flow pane using only buttons

import java.awt.Insets;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
// class that extends application class
public
class JavaFXFlowPaneExample extends Application
{
    // application starts at this point
    @Override public void start(Stage s) throws Exception
    {
        // set title
        s.setTitle("FlowPane Example");
        // create buttons
        Button bt1 = new Button("Fruits");
        Button bt2 = new Button("Vegetables");
        Button bt3 = new Button("Groceries");
        Button bt4 = new Button("Households");
        Button bt5 = new Button("Juices");
        Button bt6 = new Button("Diary products");
        // create Flow pane
        FlowPane fp = new FlowPane();
        // Set the horizontal gap
        fp.setHgap(25);
        // set the vertical gap
        fp.setVgap(10);
        // set the orientation
        fp.setOrientation(Orientation.VERTICAL);
        // observable list
        ObservableList<Node> li = fp.getChildren();
        // Add all the nodes to the flow pane
        li.addAll(bt1, bt2, bt3, bt4, bt5, bt6);
        // scene
        Scene sc = new Scene(fp, 500, 500);
        // set scene
        s.setScene(sc);
        // display the result
        s.show();
    }
    // main method
public
    static void main(String[] args)
    {
        Application.launch(args);
    }
}

 

Output

On executing the code, a dialog box appears will all the buttons arranged vertically. This is done with the help of an orientation set as vertical. Here, a vertical and horizontal gap is also set, unlike the programs mentioned above.

Frequently Asked Questions

What are the various pane types in JavaFX?

There are 6 Panels in javaFX 

  • BorderPane
  • StackPane
  • GridPane
  • FlowPane
  • TilePane 
  • AnchorPane.

How are Flowpane and VBox different?

FlowPane arranges its children in a flow that wraps around the flowpane's edge. HBox - arranges its content nodes in a single row horizontally. VBox arranges its content nodes in a single column vertically. AnchorPane - anchor nodes to the pane's top, bottom, left side, or center. 

In JavaFX, what is a node?

Node is the base class (superclass) for all JavaFX Scene Graph components. Because the JavaFX Node class is abstract, you can only add subclasses to the scene graph. The JavaFX Node class defines a set of common properties shared by all JavaFX Node instances in the scene graph.

Conclusion

In this article, we have extensively discussed the concepts of JavaFX Flowpane. We started with introducing the JavaFx Flowpane, constructors of JavaFX flowpane, and methods of JavaFX flowpane, then concluded with JavaFX flowpane implementation.

We hope that this blog has helped you enhance your knowledge regarding JavaFX flowpane, and if you would like to learn about JavaFX, you can learn from JavaFXJavaFX Fill transitionJavaFX Scale Transition, JavaFX rotate transition, etc.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! You may also check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! For placement preparations, you must look at the problemsinterview experiences, and interview bundle.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Coding!

Live masterclass