JavaFX | FlowPane Class - GeeksforGeeks

12 Sep, 2018
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:
FlowPane(): Creates a new Horizontal FlowPane layout.FlowPane(double h, double v): Creates a new Horizontal FlowPane layout, with specified horizontal and vertical gap.FlowPane(double h, double v, Node... c): Creates a new Horizontal FlowPane layout, with specified horizontal, vertical gap and nodes.FlowPane(Node... c): Creates a FlowPane with specified childrens.FlowPane(Orientation o): Creates a FlowPane with specified orientationFlowPane(Orientation o, double h, double v): Creates a FlowPane with specified orientation and specified horizontal and vertical gap.FlowPane(Orientation o, double h, double v, Node... c): Creates a FlowPane with specified orientation and specified horizontal and vertical gap and specified childrens.FlowPane(Orientation o, Node... c): Creates a FlowPane with specified orientation and specified nodes.
FlowPane(): Creates a new Horizontal FlowPane layout.
FlowPane(double h, double v): Creates a new Horizontal FlowPane layout, with specified horizontal and vertical gap.
FlowPane(double h, double v, Node... c): Creates a new Horizontal FlowPane layout, with specified horizontal, vertical gap and nodes.
FlowPane(Node... c): Creates a FlowPane with specified childrens.
FlowPane(Orientation o): Creates a FlowPane with specified orientation
FlowPane(Orientation o, double h, double v): Creates a FlowPane with specified orientation and specified horizontal and vertical gap.
FlowPane(Orientation o, double h, double v, Node... c): Creates a FlowPane with specified orientation and specified horizontal and vertical gap and specified childrens.
FlowPane(Orientation o, Node... c): Creates a FlowPane with specified orientation and specified nodes.
Commonly Used Methods:
Below programs illustrate the use of FlowPane Class:
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.// Java Program to create a FlowPane,// add labels to the flow pane// and add it to the stageimport 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);    }}Output: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.// Java Program to create a FlowPane// set its orientation, add labels // and buttons and add it to the stageimport 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 applicationpublic 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 Methodpublic static void main(String args[]){      // launch the application    launch(args);}}Output: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.// 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 stageimport 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 applicationpublic 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 Methodpublic static void main(String args[]){      // launch the application    launch(args);}}Output:
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.// Java Program to create a FlowPane,// add labels to the flow pane// and add it to the stageimport 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);    }}Output:
// Java Program to create a FlowPane,// add labels to the flow pane// and add it to the stageimport 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);    }}
Output:
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.// Java Program to create a FlowPane// set its orientation, add labels // and buttons and add it to the stageimport 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 applicationpublic 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 Methodpublic static void main(String args[]){      // launch the application    launch(args);}}Output:
// Java Program to create a FlowPane// set its orientation, add labels // and buttons and add it to the stageimport 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 applicationpublic 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 Methodpublic static void main(String args[]){      // launch the application    launch(args);}}
Output:
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.// 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 stageimport 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 applicationpublic 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 Methodpublic static void main(String args[]){      // launch the application    launch(args);}}Output:
// 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 stageimport 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 applicationpublic 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 Methodpublic static void main(String args[]){      // launch the application    launch(args);}}
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
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
How to remove an element from ArrayList in Java?
Difference between Abstract Class and Interface in Java