Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - SplitPane, resize & proportions

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?

like image 803
woky Avatar asked Mar 10 '13 16:03

woky


2 Answers

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?

like image 74
user2943543 Avatar answered Sep 25 '22 07:09

user2943543


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

    split.setDividerPositions(0.1f, 0.6f, 0.9f)
like image 36
AS3Boyan Avatar answered Sep 24 '22 07:09

AS3Boyan