JavaFX - SplitPane, resize & proportions

两盒软妹~` 提交于 2019-12-01 14:58:49

问题


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:

Set dividers how I like them:

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

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

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?


回答1:


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

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



回答2:


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?




回答3:


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
});


来源:https://stackoverflow.com/questions/15324321/javafx-splitpane-resize-proportions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!