JavaFX | FlowPane Class

FlowPane class is a part of JavaFX. Flowpane lays out its children in such a way that wraps at the flowpane’s boundary. A horizontal flowpane (the default) will layout nodes in rows, wrapping at the flowpane’s width. A vertical flowpane lays out nodes in columns, wrapping at the flowpane’s height. FlowPane class inherits Pane class.

Constructors of the class:

  1. FlowPane(): Creates a new Horizontal FlowPane layout.
  2. FlowPane(double h, double v): Creates a new Horizontal FlowPane layout, with specified horizontal and vertical gap.
  3. FlowPane(double h, double v, Node… c): Creates a new Horizontal FlowPane layout, with specified horizontal, vertical gap and nodes.
  4. FlowPane(Node… c): Creates a FlowPane with specified childrens.
  5. FlowPane(Orientation o): Creates a FlowPane with specified orientation
  6. FlowPane(Orientation o, double h, double v): Creates a FlowPane with specified orientation and specified horizontal and vertical gap.
  7. FlowPane(Orientation o, double h, double v, Node… c): Creates a FlowPane with specified orientation and specified horizontal and vertical gap and specified childrens.
  8. FlowPane(Orientation o, Node… c): Creates a FlowPane with specified orientation and specified nodes.

Commonly Used Methods:



Method Explanation
getAlignment() Returns the value of Alignment of the pane.
getHgap() Returns the horizontal gap of the flow pane.
getOrientation() Returns the orientation of the pane.
getRowValignment() Gets the value of the property rowValignment.
getVgap() Returns the vertical gap of the flow pane.
setAlignment(Pos v) Set the value of Alignment of the pane.
setHgap(double v) Sets the horizontal gap of the flow pane.
setOrientation(Orientation o) Set the orientation of the pane.
setRowValignment(double v) Sets the value of the property rowValignment.
setVgap(double v) Sets the vertical gap of the flow pane.

Below programs illustrate the use of FlowPane Class:

  1. Java Program to create a FlowPane, add labels to the flow pane and add it to the stage: In this program we will create a FlowPane and 5 Label named label, label1, label2, label3, label4. Add the labels to the flow_pane by passing it as the arguments. Set the FlowPane to the scene and add the scene to the stage. Call the show() function to display the final results.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to create a FlowPane,
    // add labels to the flow pane
    // and add it to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
      
    public class FlowPane_0 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("FlowPane");
      
                // create a labels
                Label label = new Label("this is FlowPane example");
                Label label1 = new Label("label no 1");
                Label label2 = new Label("label no 2");
                Label label3 = new Label("label no 3");
                Label label4 = new Label("label no 4");
      
                // create a FlowPane
                FlowPane flow_pane = new FlowPane(20.0, 20.0, label, label1,
                                                    label2, label3, label4);
      
                // create a scene
                Scene scene = new Scene(flow_pane, 400, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    Output:

  2. Java Program to create a FlowPane set its orientation, add labels and buttons and add it to the stage: In this program we will create a FlowPane and a Label named label. Add the label to the flow_pane by passing it through the argument, orientation and the hgap, and vgap values. Add the buttons using getChildren().add(). Set the FlowPane to the scene. Add the scene to the stage. Call the show() function to display the final results.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to create a FlowPane
    // set its orientation, add labels 
    // and buttons and add it to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.geometry.*;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
       
    public class FlowPane_1 extends Application {
       
    // launch the application
    public void start(Stage stage)
    {
      
        try {
      
            // set title for the stage
            stage.setTitle("FlowPane");
      
            // create a label
            Label label = new Label("this is FlowPane example");
      
            // create a FlowPane
            FlowPane flow_pane = new FlowPane(Orientation.VERTICAL,
                                                20.0, 20.0, label);
      
            // add buttons
            for (int i = 0; i < 10; i++) {
      
                // add nodes to the flow pane
                flow_pane.getChildren().add(new Button("Button " 
                                                + (int)(i + 1)));
            }
      
            // create a scene
            Scene scene = new Scene(flow_pane, 400, 300);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        catch (Exception e) {
      
            System.out.println(e.getMessage());
        }
    }
      
    // Main Method
    public static void main(String args[])
    {
      
        // launch the application
        launch(args);
    }
    }

    chevron_right

    
    

    Output:

  3. Java Program to create a FlowPane set its orientation, add labels and buttons, set the alignment, column alignment, row alignment of the FlowPane and add it to the stage: In this program we will create a FlowPane and a Label named label. Add the label to the flow_pane by passing it through the argument, orientation, and the hgap, and vgap values. Now add the buttons using getChildren().add(). Set the FlowPane to the scene. Set the alignment of the FlowPane using functions using setAlignment(), setColumnHalignment(), setRowValignment(). Add the scene to the stage. Call the show() function to display the final results.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to create a FlowPane set its orientation,
    // add labels and buttons, set the alignment, column 
    // alignment, row alignment of the FlowPane and add it 
    // to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.geometry.*;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
      
    public class FlowPane_2 extends Application {
      
    // launch the application
    public void start(Stage stage)
    {
      
        try {
      
            // set title for the stage
            stage.setTitle("FlowPane");
      
            // create a label
            Label label = new Label("this is FlowPane example");
      
            // create a FlowPane
            FlowPane flow_pane = new FlowPane(Orientation.VERTICAL,
                                                20.0, 20.0, label);
      
            // add buttons
            for (int i = 0; i < 10; i++) {
                // add nodes to the flow pane
                flow_pane.getChildren().add(new Button("Button " 
                                               + (int)(i + 1)));
            }
      
            // set alignment of flow pane
            flow_pane.setAlignment(Pos.CENTER);
            flow_pane.setColumnHalignment(HPos.CENTER);
            flow_pane.setRowValignment(VPos.CENTER);
      
            // create a scene
            Scene scene = new Scene(flow_pane, 400, 300);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        catch (Exception e) {
      
            System.out.println(e.getMessage());
        }
    }
      
    // Main Method
    public static void main(String args[])
    {
      
        // launch the application
        launch(args);
    }
    }

    chevron_right

    
    

    Output:

Note: The above programs might not run in an online IDE please use an offline compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/FlowPane.html



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.