JavaFX | SplitPane Class

SplitPane class is a part of JavaFX. SplitPane class is a control which contains two or more sides separated by a divider. Sides can be dragged by the user to give more space to one of the sides which cause the other side to shrink by an equal amount. SplitPane class inherits Control class.
Constructor of the Class:
- SplitPane(): Creates a new SplitPane.
- SplitPane(Node⦠n): Creates a SplitPane with specified nodes.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getItems() | Returns the items of the split pane. |
| getOrientation() | Returns the orientation of split pane. |
| setDividerPosition(int dividerIndex, double position) | Sets the position of divider at specified index. |
| setDividerPositions(double⦠p) | Sets the position of dividers. |
| setOrientation(Orientation o) | Sets the orienattion of splitpane. |
Below programs illustrate the use of SplitPane Class:
- Java program to create a split pane and add labels to it:
- In this program, we will create a SplitPane name split_pane.
- Create and add labels to the split pane using getItems().add() function.
- Add the split_pane to the scene and add the scene to the stage.
- Call the show() function to display the final results.
// Java program to create a split pane// and add labels to itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;importjavafx.scene.paint.*;importjavafx.scene.*;importjava.io.*;importjavafx.scene.image.*;ĀĀpublicclassSplitPane_1extendsApplication {ĀĀĀ Ā Ā Ā// launch the applicationĀ Ā Ā Āpublicvoidstart(Stage stage)Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ātry{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set title for the stageĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setTitle("Split Pane");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create a splitpaneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀSplitPane split_pane =newSplitPane();ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create labels and add it to splitPaneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āfor(inti =1; i <5; i++) {ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āsplit_pane.getItems().add(newLabel("\tLabel no "ĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā+ i +"\t"));Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create a sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀScene scene =newScene(split_pane,500,300);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setScene(scene);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.show();Ā Ā Ā Ā Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā Ā Ā Ā Ācatch(Exception e) {ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀSystem.out.println(e.getMessage());Ā Ā Ā Ā Ā Ā Ā Ā}Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā// Main MethodĀ Ā Ā Āpublicstaticvoidmain(String args[])Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā// launch the applicationĀ Ā Ā Ā Ā Ā Ā Ālaunch(args);Ā Ā Ā Ā}}Output:
- Java program to create a split pane set its orientation and add labels to it:
- In this program we will create a SplitPane name split_pane.
- Create and add labels to the split pane using getItems().add() function.
- Add the split_pane to the scene and add the scene to the stage.
- Set the orientation of the split_pane using the setOrientation() function.
- Call the show() function to display the final results.
// Java program to create a split pane, set// its orientation and add labels to itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;importjavafx.scene.paint.*;importjavafx.scene.*;importjava.io.*;importjavafx.scene.image.*;ĀĀpublicclassSplitPane_2extendsApplication {ĀĀĀ Ā Ā Ā// launch the applicationĀ Ā Ā Āpublicvoidstart(Stage stage)Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ātry{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set title for the stageĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setTitle("Split Pane");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create a splitpaneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀSplitPane split_pane =newSplitPane();ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create labels and add it to splitPaneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āfor(inti =1; i <5; i++) {ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create a labelĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀLabel label =newLabel("\tLabel no "+ i +"\t");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set preferred heightĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ālabel.setPrefHeight(50);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āsplit_pane.getItems().add(label);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set Orientation of splitpaneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āsplit_pane.setOrientation(Orientation.VERTICAL);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create a sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀScene scene =newScene(split_pane,500,300);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setScene(scene);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.show();Ā Ā Ā Ā Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā Ā Ā Ā Ācatch(Exception e) {ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀSystem.out.println(e.getMessage());Ā Ā Ā Ā Ā Ā Ā Ā}Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā// Main MethodĀ Ā Ā Āpublicstaticvoidmain(String args[])Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā// launch the applicationĀ Ā Ā Ā Ā Ā Ā Ālaunch(args);Ā Ā Ā Ā}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/SplitPane.html




