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

