The FlowPane layouts nodes in a row based on the available horizontal spacing and wraps nodes to the next line when horizontal space is less than the total of all the nodes' widths.
By default a FlowPane layout flows child nodes from left to right (Pos.TOP_LEFT).
To change the flow alignment, call the setAlignment()
method
by passing in an enumerated value of type Pos
.
The following code creates a FlowPane layout to flow child nodes from right to left (Pos.TOP_RIGHT).
FlowPane flowPane = new FlowPane();
flowPane.setAlignment(Pos.TOP_RIGHT);
flowPane.getChildren().addAll(...); // child nodes to add.
Adding Buttons to flow pane
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; /*from w w w .ja va2 s . c o m*/ public class Main extends Application { @Override public void start(Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); FlowPane flow = new FlowPane(); flow.setVgap(8); flow.setHgap(4); flow.setPrefWrapLength(300); // preferred width = 300 for (int i = 0; i < 10; i++) { flow.getChildren().add(new Button("asdf")); } scene.setRoot(flow); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The code above generates the following result.
FlowPane preferred width allows for two columns
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; //from w w w. j a v a 2 s. c om public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("FlowPane example"); FlowPane flowPane = new FlowPane(); flowPane.setPadding(new Insets(10, 10, 10, 10)); flowPane.setVgap(4); flowPane.setHgap(4); flowPane.setPrefWrapLength(210); Button btn = new Button(); for (int i = 0; i < 8; i++) { btn = new Button("Button"); btn.setPrefSize(100, 50); flowPane.getChildren().add(btn); } Scene scene = new Scene(flowPane); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.