How to lock splitter in SplitPane JavaFX?

I have a SplitPane and I need to split the layout 25% and 75%. In addition, I need to prohibit dragging to the right side for 25%. However, I can drag it to 25% of the space. Please, help.

+4
source share
1 answer

SplitPanewill take into account the minimum and maximum dimensions of the components ( items) that it contains. To get the desired behavior, bind the maxWidthleft 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);
    }
}
+10
source

All Articles