The tile pane layout is similar to the FlowPane layout. TilePane places all of the nodes in a grid in which each cell, or tile, is the same size. It arranges nodes in neat rows and columns, either horizontally or vertically.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("TilePane Demo");
double width = 400;
double height = 300;
TilePane root = new TilePane();
root.setStyle("-fx-background-color:blue");
// to set horizontal and vertical gap
root.setHgap(20);
root.setVgap(50);
Button bl = new Button("Buttons");
root.getChildren().add(bl);
Button btn = new Button("Button");
root.getChildren().add(btn);
Button btn1 = new Button("Button 1");
root.getChildren().add(btn1);
Button btn2 = new Button("Button 2");
root.getChildren().add(btn2);
Button btn3 = new Button("Button 3");
root.getChildren().add(btn3);
Button btn4 = new Button("Button 4");
root.getChildren().add(btn4);
Scene scene = new Scene(root, width, height);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
output
To create Tilepane
TilePane root = new TilePane();
setHgap() And setVgap() method is used to make gap between column and column. we can also set the columns for the layout by using
int columnCount = 2;
root.setPrefColumns(columnCount);