Search code examples
javafxsplitpane

JavaFX: Splitpane columns not equal


I want to build a javafx application, that consists of 3 big columns about the whole scene. The columns should be resizable, so i used splitpane. I added 3 borderpanes to the splitpane, every with the same preferred width (The scene is overall 3840 width, so each column is 1280 width). But when i start the application, the middle borderpane is smaller then the two other. It is not scaled on 1280 width.

So how can i manage that the splitpane is not scaling the borderpanes and use the preferred width?

Update: I have found a good solution, that makes the solution from user3249346 dynamic. After adding or deleting a column you can call this:

ObservableList<SplitPane.Divider> dividers = splitPane.getDividers();
for (int i = 0; i < dividers.size(); i++) {
    dividers.get(i).setPosition((i + 1.0) / 3);
}

Solution

  • I tried this and this works when i maximize and restore the screen with three columns of Splitpane. Important is the Platform.runLater

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.SplitPane;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class SplitPaneTest  extends Application {
    @Override public void start(Stage stage) {
         final SplitPane sp = new SplitPane();
         final StackPane sp1 = new StackPane();
         Button addColumn = new Button("Add Column");
           // Added the below function to set the size dynamically when column is added.
         addColumn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                StackPane temp = new StackPane();
                temp.getChildren().add(new Button("New Column"));
                sp.getItems().add(temp);
                int divCount = sp.getDividerPositions().length;
                double equalSize = 1.0/(divCount+1);
                double divPosValues[] = new double[divCount];
                for(int count=1; count<divCount+1; count++) {
                    divPosValues[count-1] = equalSize*count;
                }
                sp.setDividerPositions(divPosValues);
            }
        });
    
         sp1.getChildren().add(addColumn);
         final StackPane sp2 = new StackPane();
         sp2.getChildren().add(new Button("New Button"));
         final StackPane sp3 = new StackPane();
         sp3.getChildren().add(new Button("New Button"));
         sp.getItems().addAll(sp1, sp2, sp3);
         sp.setDividerPositions(0.33f, 0.63f, 0.93f);
    
        Scene scene = new Scene(sp, 400, 400);
        stage.setScene(scene);
    
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                sp.setDividerPositions(0.3f, 0.6f, 0.9f);
            }
          });
        stage.show();
    }
    
    public static void main(String[] args) {
        Application.launch(args);
    }
    }