How to bind divider position to pixels instead of percentage on SplitPanes in JavaFX?

1k views Asked by At

After resizing the window my SplitPane's divider moves to another position according to the given percentage (e.g.: dividerPositions="0.05").

But I want it to stay on the same position after resizing (like it was bound to pixel size and not percentage) where it was set before. I don't want it "move" (follow the percentage constraint) while resizing it's parent pane.

Is it possible to do this somehow? (I would like to fix this preferably from code, the CSS solution is secondary for me)

The perfect solution for me would something be similar to this:

<SplitPane orientation="VERTICAL" dividerPositionFromTop="100px" 
           AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" 
           AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" > 
... 
</SplitPane>

Where dividerPositionFromTop sadly does not exist by my knowledge.

1

There are 1 answers

0
user7291698 On

You can add a listener to the heightProperty of your SplitPane and recalculate the percentage value:

SplitPane splitPane = new SplitPane();

splitPane.heightProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
        double height = splitPane.getHeight();
        double dividerPositionFromTop = 100;
        splitPane.setDividerPosition(0, dividerPositionFromTop/height);
    }
});
splitPane.getItems().addAll(new TextArea("1"), new TextArea("2"));
splitPane.setOrientation(Orientation.VERTICAL);