设置SplitPane的分隔位置

Mid*_*ard 6 javafx javafx-8

我想将SplitPane的分隔符设置为某个默认位置.这不起作用,分隔线保持在中间:

public void start(Stage primaryStage) throws Exception{

    SplitPane splitPane = new SplitPane(new Pane(), new Pane());

    // Report changes to the divider position
    splitPane.getDividers().get(0).positionProperty().addListener(
            o -> System.out.println(splitPane.getDividerPositions()[0])
    );

    // Doesn't work:
    splitPane.setDividerPositions(0.8);

    // The docs seem to recommend the following (with floats instead of
    // doubles, and with one number more than there are dividers, which is
    // weird), but it doesn't work either:
    //splitPane.setDividerPositions(0.8f, 0.2f);

    primaryStage.setScene(new Scene(splitPane));
    primaryStage.setMaximized(true);
    primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)

输出:

0.8
0.5
Run Code Online (Sandbox Code Playgroud)

结果截图

它表明某些东西会重置到中间.

我怎样才能做到这一点?

fab*_*ian 11

问题似乎SplitPane是在最大化期间设置宽度时重置分隔符位置Stage.您应该可以通过监听showing窗口的属性来设置它:

primaryStage.showingProperty().addListener(new ChangeListener<Boolean>() {

    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        if (newValue) {
            splitPane.setDividerPositions(0.8);
            observable.removeListener(this);
        }
    }

});
Run Code Online (Sandbox Code Playgroud)


Mar*_*ark -3

尝试

splitPane.setDividerPosition(0, percentage);
Run Code Online (Sandbox Code Playgroud)

参数为setDividerPosition(intdividerIndex,doublepercentage)