'How to lock the divider in SplitPane JavaFX?

I have a SplitPane and I need to divide the layout 25% and 75%. Also, I need to disallow dragging towards right side beyond the 25% split. However I can drag to any extent within the 25% space. Please help.



Solution 1:[1]

SplitPane will respect the min and max dimensions of the components (items) it contains. So to get the behavior you want, bind the maxWidth of the left component to splitPane.maxWidthProperty().multiply(0.25):

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ConstrainedSplitPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane leftPane = new StackPane(new Label("Left"));
        StackPane rightPane = new StackPane(new Label("Right"));
        SplitPane splitPane = new SplitPane();
        splitPane.getItems().addAll(leftPane, rightPane);
        splitPane.setDividerPositions(0.25);

        //Constrain max size of left component:
        leftPane.maxWidthProperty().bind(splitPane.widthProperty().multiply(0.25));

        primaryStage.setScene(new Scene(new BorderPane(splitPane), 800, 600));
        primaryStage.show();
    }

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

Solution 2:[2]

This worked for me

private final double dividerMaxWidth = .15;
splitPane.getDividers().get(0).positionProperty().addListener((observable,oldValue,newValue) -> {
    if(splitPane.getDividers().get(0).getPosition() > dividerMaxWidth)
        splitPane.setDividerPosition(0, dividerMaxWidth);
});

And if you want to lock a divider into a hard position that cannot be moved

private final double absolutePosition = .15;
splitPane.getDividers().get(0).positionProperty().addListener((observable,oldValue,newValue) -> {
    splitPane.setDividerPosition(0, absolutePosition);
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James_D
Solution 2 Michael Sims