JavaFX - SplitPane, resize & proportions
Asked Answered
P

3

11

How can I achieve proportional resize of whole SplitPane?

public class JfxSplitPaneTest extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        SplitPane split = new SplitPane();
        StackPane child1 = new StackPane();
        child1.setStyle("-fx-background-color: #0000AA;");
        StackPane child2 = new StackPane();
        child2.setStyle("-fx-background-color: #00AA00;");
        StackPane child3 = new StackPane();
        child3.setStyle("-fx-background-color: #AA0000;");
        split.getItems().addAll(child1, child2, child3);
        //split.setDividerPositions(0.1f, 0.6f, 0.9f)
        stage.setScene(new Scene(split, 400, 300));
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Start the program:

enter image description here

Set dividers how I like them:

enter image description here

Make window width really small (I continued and made it even smaller, no pic):

enter image description here

Resize back and observe that dividers are neither how I set them nor how they were when program started.

enter image description here

Doing this makes it closer to what I'd expect:

    child1.setMinWidth(Region.USE_PREF_SIZE)
    child1.setPrefWidth(100)
    child2.setMinWidth(Region.USE_PREF_SIZE)
    child2.setPrefWidth(200)
    child3.setMinWidth(Region.USE_PREF_SIZE)
    child3.setPrefWidth(300)

except that dividers are initally at wrong position (until I resize SplitPane, 1px is enough) and when shrinking window width, dividers are inside components.

How can I make this work please?

Protagoras answered 10/3, 2013 at 16:4 Comment(0)
D
4

Try to set the divisors like AS3Boyan said

split.setDividerPositions(0.1f, 0.6f, 0.9f)

and add this:

How can I avoid a SplitPane to resize one of the panes when the window resizes?

Doubtless answered 20/11, 2017 at 23:18 Comment(1)
The link you posted has the correct solution that worked for me: SplitPane.setResizableWithParent(false)Dharana
E
2

Try to add Change Listener to stage.widthProperty() and stage.heightProperty(). And call this:

    split.setDividerPositions(0.1f, 0.6f, 0.9f)
Equitable answered 27/9, 2013 at 11:34 Comment(0)
C
0

The rule of thumb is to use setDividerPositions with Platform.runLater when the resize event is triggered.

You probably want to start with a default ratio, let the user change that ratio, and maintain this ratio whatever it is when there's a resize event. Let's consider a 25/75 vertical FX splitPane in some stage:

splitPane.setDividerPositions(0.25f, 0.75f);
stage.heightProperty().addListener((obs, oldVal, newVal) -> {
    double[] positions = splitPane.getDividerPositions(); // reccord the current ratio
    Platform.runLater(() -> splitPane.setDividerPositions(positions)); // apply the now former ratio
});
Cahn answered 14/3, 2018 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.