FreeCode

Donate HTML/CSS JavaFX Java Home
JavaFX Starter Template JavaFX Labels JavaFX Buttons JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups JavaFX Checkboxes JavaFX Choice Boxes JavaFX Combo Boxes JavaFX Text Areas JavaFX Text Fields JavaFX List Views JavaFX Image Views JavaFX Color Pickers JavaFX Date Pickers JavaFX Resizable Split Pane Layouts JavaFX VBox (Vertical Box Layouts) JavaFX HBox (Horizontal Box Layouts) JavaFX Flow Pane Layouts JavaFX Tile Pane Layouts JavaFX Web Views

JavaFX Resizable Split Pane Layouts

Code Description:

The code provided below shows how to create resizable container which can be set to move either horizontally or vertically. In our runnable example, we have it configured to horizontal.

Split Pane Syntax:

SplitPane splitPane = new SplitPane();
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.getItems().addAll(container1, container2 );

Runnable JavaFX Split Pane Example Code:

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Demo extends Application {

    public static void main(String[] args) {
	launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
		
        SplitPane splitPane = new SplitPane();
	splitPane.setOrientation(Orientation.HORIZONTAL);

	Button button1 = new Button("Button1");
	Button button2 = new Button("Button2");

	VBox vBox1 = new VBox();
	vBox1.getChildren().add(button1);
		
	VBox vBox2 = new VBox();
	vBox2.getChildren().add(button2);

	splitPane.getItems().addAll(vBox1, vBox2);
			
	primaryStage.setTitle("Split Pane Demo");
	primaryStage.setScene(new Scene(splitPane, 400, 400));
	primaryStage.show();
		
    }

}

How to Run This Code:

Make sure that your Class name is the same as ours. In this case, we are using the Class name of "Demo". Alternatively, you can change the Class name in the provided code to match yours.

Note: You will NOT be able to copy and paste this code into your IDE unless you first set up JavaFX for your project. Check out this link if you need help setting up JavaFX:

JavaFX Comprehensive Setup Guide

Output:

With Horizontal Orientation

JavaFX Horizontal Split Pane Output Image

With Vertical Orientation

JavaFX Vertical Split Pane Output Image

Important Methods:

  • setOrientation() - Used to set if the split bars will move vertically or horizontally. EX: "splitPane.setOrientation(Orientation.VERTICAL);"
  • getItems().addAll() - Add all specified items to be separated in the split pane.
  • setDisable() - Used to make the split pane grayed out and unable to be resized. EX: "splitPane.setDisable(true);"

Related Links:

JavaFX VBox (Vertical Box Layouts) JavaFX HBox (Horizontal Box Layouts) JavaFX Flow Pane Layouts JavaFX Tile Pane Layouts