JavaFX - FlowPane



If we use flow pane in our application, all the nodes are wrapped in a flow. A horizontal flow pane wraps the elements of the pane at its height, while a vertical flow pane wraps the elements at its width.

The class named FlowPane of the package javafx.scene.layout represents the Flow Pane. This class contains 7 properties, which includes −

  • alignment − This property represents the alignment of the contents of the Flow pane. You can set this property using the setter method setAllignment().

  • columnHalignment − This property represents the horizontal alignments of nodes in a vertical flow pane.

  • rowValignment − This property represents the vertical alignment of nodes in a horizontal flow pane.

  • Hgap − This property is of double type and it represents the horizontal gap between the rows/columns of a flow pane.

  • Orientation − This property represents the orientation of a flow pane.

  • Vgap − This property is of double type and it represents the vertical gap between the rows/columns of a flow pane.

Example

The following program is an example of the FlowPane layout. In this, we are inserting four button in the horizontal flow pane.

Save this code in a file with the name FlowPaneExample.java.

import javafx.collections.ObservableList; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.shape.Sphere; 
import javafx.stage.Stage; 
         
public class FlowPaneExample extends Application { 
   @Override 
   public void start(Stage stage) {      
      //Creating button1 
      Button button1 = new Button("Button1");       
      
      //Creating button2 
      Button button2 = new Button("Button2");       
      
      //Creating button3
      Button button3 = new Button("Button3");       
      
      //Creating button4 
      Button button4 = new Button("Button4");       
      
      //Creating a Flow Pane 
      FlowPane flowPane = new FlowPane();    
       
      //Setting the horizontal gap between the nodes 
      flowPane.setHgap(25); 
       
      //Setting the margin of the pane  
      flowPane.setMargin(button1, new Insets(20, 0, 20, 20)); 
       
      //Retrieving the observable list of the flow Pane 
      ObservableList list = flowPane.getChildren(); 
      
      //Adding all the nodes to the flow pane 
      list.addAll(button1, button2, button3, button4); 
        
      //Creating a scene object 
      Scene scene = new Scene(flowPane);  
      
      //Setting title to the Stage 
      stage.setTitle("Flow Pane Example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){
      launch(args); 
   } 
}

Compile and execute the saved java file from the command prompt using the following commands.

javac FlowPaneExample.java 
java FlowPaneExample

On executing, the above program generates a JavaFX window as shown below.

FlowPane
javafx_layout_panes.htm
Advertisements