TilePane Overview

106 67

The TilePane Class

Overview


The TilePane class creates a JavaFX layout manager that positions all of its child nodes in a grid of uniformly sized "tiles" (really we're talking about squares. For example, a chessboard has a layout of uniformly sized squares.) The size of the tiles is dependent on the size of largest child node contained within the TilePane. The size of the tiles will change when a child node is added that is larger than the previously added child nodes.

The default number of rows and columns is 5. This can be set to be a preferred column and row size but this will generally be dependent on the size of the TilePane itself.

Import Statement


import javafx.scene.layout.TilePane;

Constructors


The TilePane class has four constructors. Each can be used to specify the configuration of the TilePane object you wish to create.

The first constructor creates a horizontal layout for the TilePane with the prefColumns property set at 5 and the hgap and vgap properties set to 0. This constructor is the default settings for a TilePane and takes no arguments :

TilePane tiles = new TilePane();
The next constructor accepts values for the hgap and vgap properties:

TilePane tiles = new TilePane(10,10);
The third constructor can be used to set the orientation of the TilePane object. Like the first constructor is sets the property values of prefColumns = 5, hgap = 0 and vgap = 0:

TilePane tiles = new TilePane(Orientation.VERTICAL);
Finally the last constructor can be passed values for the orientation, vgap and hgap properties:

TilePane tiles = new TilePane(Orientation.VERTICAL,10,10);

Useful Methods

The alignment for a child node contained within a tile in a TilePane can be set using the setAlignment method:

Text tueDay = new Text("Tue");tueDay.setFont(Font.font("Arial", FontWeight.BOLD, 20));tiles.getChildren().add(tueDay);TilePane.setAlignment(tueDay, Pos.CENTER_LEFT);
Note: the setAlignment method is a static method and is accessed from the TilePane class. It uses the  Pos enum to define twelve positions for the horizontal and vertical positioning of the node.

The amount of vertical and horizontal space between each tile can be set by the setVgap and setHgap methods:

tiles.setVgap(3);tiles.setHgap(3);
As you would expect there are corresponding methods getVgap and getHgap for finding out the values the vgap and hgap properties have been set to.

If you want to file out the height and width the tiles have been set to after the child nodes have been added and the TilePane has been displayed then use the getTileHeight and getTileWidth methods:

System.out.println(tiles.getTileHeight() + " " + tiles.getTileWidth());

Usage Tips


Setting the preferred number of rows for a TilePane with vertical orientation and the preferred number of columns for a TilePane with horizontal orientation is recommended. This can be done by using the setPrefRows and setPrefColumns methods:

tiles.setPrefRows(7);tiles.setPrefColumns(7);
There are corresponding methods getPrefRows and getPrefColumns if you want to look up the values that have been set.

To see the TilePane layout in action have a look at the TilePane Example Program.
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.