FlowPane
lays out nodes in rows or columns based on the available horizontal or vertical space available. It wraps nodes to the next line when the horizontal space is less than the total of all the nodes' widths; it wraps nodes to the next column when the vertical space is less than the total of all the nodes' heights. This example illustrates the default horizontal layout:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FlowPane root = new FlowPane();
for (int i=1; i<=15; i++) {
Button b1=new Button("Button "+String.valueOf(i));
root.getChildren().add(b1); //for adding button to root
}
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("FlowPane Layout");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Default
FlowPane
constructor:
FlowPane root = new FlowPane();
Additional
FlowPane
constructors:
FlowPane() //Creates a horizontal FlowPane layout with hgap/vgap = 0 by default.
FlowPane(double hgap, double gap) //Creates a horizontal FlowPane layout with the specified hgap/vgap.
FlowPane(double hgap, double vgap, Node... children) //Creates a horizontal FlowPane layout with the specified hgap/vgap.
FlowPane(Node... children) //Creates a horizontal FlowPane layout with hgap/vgap = 0.
FlowPane(Orientation orientation) //Creates a FlowPane layout with the specified orientation and hgap/vgap = 0.
FlowPane(Orientation orientation, double hgap, double gap) //Creates a FlowPane layout with the specified orientation and hgap/vgap.
FlowPane(Orientation orientation, double hgap, double vgap, Node... children) //Creates a FlowPane layout with the specified orientation and hgap/vgap.
FlowPane(Orientation orientation, Node... children) //Creates a FlowPane layout with the specified orientation and hgap/vgap = 0.
Adding nodes to the layout uses the
add()
oraddAll()
methods of the parentPane
:
Button btn = new Button("Demo Button");
root.getChildren().add(btn);
root.getChildren().addAll(…);
By default, a FlowPane
lays out child nodes from left to right. To change the flow alignment, call the setAlignment()
method by passing in an enumerated value of type Pos
.
Some commonly used flow alignments:
root.setAlignment(Pos.TOP_RIGHT); //for top right
root.setAlignment(Pos.TOP_CENTER); //for top Center
root.setAlignment(Pos.CENTER); //for Center
root.setAlignment(Pos.BOTTOM_RIGHT); //for bottom right