How to create a SplitPane in JavaFX?


A SplitPane is a UI component that contains two or more sides with a separator in between. This separator is movable; you can reduce/increase the area of a side using it. You can create a split pane by instantiating the javafx.scene.control.SplitPane class.

The sides of the SplitPane can be arranged either horizontally or vertically. By default, the SpliPane created is horizontal you can change its orientation using the setOrientation() method.

Example

The following Example demonstrates the creation of a SplitPane.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SplitPaneExample extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating ImageView object1
      Image img1 = new Image(new FileInputStream("D:\images\elephant.jpg"));
      ImageView view1 = new ImageView(img1);
      view1.setFitWidth(250);
      view1.setFitHeight(150);
      //Creating ImageView object2
      Image img2 = new Image(new FileInputStream("D:\images\boy.jpg"));
      ImageView view2 = new ImageView(img2);
      view2.setFitWidth(250);
      view2.setFitHeight(150);
      //Creating a SplitPane
      SplitPane splitPane = new SplitPane();
      //Creating stack panes holding the ImageView objects
      StackPane stackPane1 = new StackPane(view1);
      StackPane stackPane2 = new StackPane(view2);
      //Adding the stackpanes to the splitpane
      splitPane.getItems().addAll(stackPane1, stackPane2);
      //Setting anchor pane as the layout
      AnchorPane pane = new AnchorPane();
      AnchorPane.setTopAnchor(splitPane, 15.0);
      AnchorPane.setRightAnchor(splitPane, 15.0);
      AnchorPane.setBottomAnchor(splitPane, 15.0);
      AnchorPane.setLeftAnchor(splitPane, 15.0);
      pane.getChildren().addAll(splitPane);
      pane.setStyle("-fx-background-color: BEIGE");
      //Setting the stage
      Scene scene = new Scene(pane, 595, 300);
      stage.setTitle("Split Pane");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 20-May-2020

544 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements