How to change the orientation of nodes in a tile pane using JavaFX?

JavaFXObject Oriented ProgrammingProgramming

<p>In the TilePane layout, the nodes are arranged as a grid of uniformly sized tiles. You can create a tile pane in your application by instantiating the <strong>javafx.scene.layout.TilePane</strong> class.</p><p>Orientation refers to the arrangement of the nodes in the pane in general, they are arranged wither horizontally or vertically.</p><p>By default the orientation of the tile pane is horizontal. You can change this using the <strong>setOrientation()</strong> method. This method accepts two values &minus;</p><ul class="list"><li><p>Orientation.VERTICAL</p></li><li><p>Orientation.HORIZONTAL</p></li></ul><h2>Example</h2><pre class="prettyprint notranslate" style="">import javafx.application.Application; import javafx.collections.ObservableList; import javafx.geometry.Orientation; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.TilePane; import javafx.stage.Stage; public class TilePaneOrientation extends Application { &nbsp; &nbsp;@Override &nbsp; &nbsp;public void start(Stage stage) { &nbsp; &nbsp; &nbsp; //Creating buttons &nbsp; &nbsp; &nbsp; Button one = new Button(&quot;one&quot;); &nbsp; &nbsp; &nbsp; one.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button two = new Button(&quot;Two&quot;); &nbsp; &nbsp; &nbsp; two.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button three = new Button(&quot;Three&quot;); &nbsp; &nbsp; &nbsp; three.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button four = new Button(&quot;Four&quot;); &nbsp; &nbsp; &nbsp; four.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button five = new Button(&quot;Five&quot;); &nbsp; &nbsp; &nbsp; five.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button six = new Button(&quot;six&quot;); &nbsp; &nbsp; &nbsp; six.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button seven = new Button(&quot;seven&quot;); &nbsp; &nbsp; &nbsp; seven.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button eight = new Button(&quot;eight&quot;); &nbsp; &nbsp; &nbsp; eight.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; Button nine = new Button(&quot;nine&quot;); &nbsp; &nbsp; &nbsp; nine.setPrefSize(100, 100); &nbsp; &nbsp; &nbsp; //Creating the tile pane &nbsp; &nbsp; &nbsp; TilePane tilePane = new TilePane(); &nbsp; &nbsp; &nbsp; //Setting the orientation for the Tile Pane &nbsp; &nbsp; &nbsp; tilePane.setOrientation(Orientation.VERTICAL); &nbsp; &nbsp; &nbsp; //Setting the alignment for the Tile Pane &nbsp; &nbsp; &nbsp; tilePane.setTileAlignment(Pos.BASELINE_CENTER); &nbsp; &nbsp; &nbsp; //Setting the preferred columns for the Tile Pane &nbsp; &nbsp; &nbsp; tilePane.setPrefRows(3); &nbsp; &nbsp; &nbsp; //Retrieving the observable list of the Tile Pane &nbsp; &nbsp; &nbsp; ObservableList list = tilePane.getChildren(); &nbsp; &nbsp; &nbsp; //Adding the array of buttons to the pane &nbsp; &nbsp; &nbsp; list.addAll(one, two, three, four, five, six, seven, eight, nine); &nbsp; &nbsp; &nbsp; //Setting the Scene &nbsp; &nbsp; &nbsp; Scene scene = new Scene(tilePane, 600, 300); &nbsp; &nbsp; &nbsp; stage.setTitle(&quot;Tile Pane&quot;); &nbsp; &nbsp; &nbsp; stage.setScene(scene); &nbsp; &nbsp; &nbsp;; &nbsp; &nbsp;} &nbsp; &nbsp;public static void main(String args[]){ &nbsp; &nbsp; &nbsp; launch(args); &nbsp; &nbsp;} }</pre><h2>Output</h2><p><em>Orientation.VERTICAL</em></p><p style=""><img src="" class="fr-fic fr-dib" width="600" height="333"></p><p><em>Orientation.HORIZONTAL</em></p><p style=""><img src="" class="fr-fic fr-dib" width="600" height="331"></p>
Updated on 20-May-2020 06:48:15