36 JavaFX SplitPane
The JavaFX SplitPane is a container control that can contain multiple other components inside it. In other words, the SplitPane
is split between the controls it contains. Between the controls in the SplitPane is a divider. The user can move the divider to set how much space is allocated to each control. Here is a screenshot of a JavaFX SplitPane
:
Full JavaFX SplitPane Example
The JavaFX SplitPane is represented by the JavaFX class javafx.scene.control.SplitPane
. Here is a full JavaFX SplitPane
example so you can get an idea about how using it looks:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.SplitPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class SplitPaneExample extends Application { public static void main(String[] args) { launch(args); } public void start(Stage primaryStage) { SplitPane splitPane = new SplitPane(); VBox leftControl = new VBox(new Label("Left Control")); VBox rightControl = new VBox(new Label("Right Control")); splitPane.getItems().addAll(leftControl, rightControl); Scene scene = new Scene(splitPane); primaryStage.setScene(scene); primaryStage.setTitle("JavaFX App"); primaryStage.show(); } }
Create a SplitPane
Before you can use a JavaFX SplitPlane
you must first create a SplitPane
instance. Here is an example of creating a JavaFX SplitPane
:
SplitPane splitPane = new SplitPane();
Adding Controls to the SplitPane
In order to show anything inside the JavaFX SplitPane
you must add some JavaFX controls to it. You do so via the SplitPane
getItems().add(...)
method. Here is an example of adding two controls to a JavaFX SplitPane
:
SplitPane splitPane = new SplitPane(); VBox leftControl = new VBox(new Label("Left Control")); VBox rightControl = new VBox(new Label("Right Control")); splitPane.getItems().addAll(leftControl, rightControl);
Adding More Than Two Controls to a SplitPane
You can add more than two controls to a JavaFX SplitPane
. If you do, there will be a divider in-between each two controls. Here is a Java code example of adding 3 controls to a JavaFX SplitPane
:
SplitPane splitPane = new SplitPane(); VBox leftControl = new VBox(new Label("Left Control")); VBox midControl = new VBox(new Label("Mid Control")); VBox rightControl = new VBox(new Label("Right Control")); splitPane.getItems().addAll(leftControl, midControl, rightControl); Scene scene = new Scene(splitPane); primaryStage.setScene(scene); primaryStage.setTitle("JavaFX App"); primaryStage.show();
Here is a screenshot of how such a SplitPane
with 3 controls added looks: