JavaFX TilePane Layout

0

TilePane Layout tutorial:

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.

Example of tile pane layout:


package javafxtuts;


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;


/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {
    
    

  @Override
  public void start(Stage primaryStage) {
      primaryStage.setTitle("javafxtuts.com");
      double width = 400;
      double height = 300;
      TilePane root = new TilePane();
      
      // 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();
  }
  

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output :

Explanation of tile pane example:

    • To use TitlePane as layout we make an object of TitlePane AS

TilePane root = new TilePane();

    • setHgap() And setVgap() method is used to make gap between column and column (Or row and row). As we can say horizontal and vertical gap.
    • If we add a method setPrefColumns(no.) , it means we can use only 2 columns for root . As

root.setPrefColumns(2);

  • Then Output changed AS
Share.

Leave A Reply