0

I want to synchronize dividers in SplitPane, when divider(0) moves, I also want to make the same move by divider(1). I guess I have to bind the positionProperty of divider(0) with something.

How can I achieve this?

SedJ601
  • 12,173
  • 3
  • 41
  • 59
G.Kot
  • 63
  • 1
  • 1
  • 7
  • This would imply the second item in the split pane always has the same size. You can achieve this simply by setting its min and max width (or height, for a vertical split pane) to the same value, so that it can't be resized. – James_D Mar 11 '18 at 22:40
  • I don't want to have the same size for both items. When I expand the first item, I don't want to change the size of the second one. I just want to make move by the second item in this situaton. – G.Kot Mar 12 '18 at 11:49
  • That's what I meant: perhaps I should have said "The second item in the split pane always has a *fixed* size.". – James_D Mar 12 '18 at 11:50
  • Well, experiments suggest that doesn't work; it just effectively disables the dividers. – James_D Mar 12 '18 at 12:12

1 Answers1

0

You need to add listeners to the positions of each divider, and update the "linked" divider when it changes. It's important to make sure you don't end up in an infinite recursive loop; the simplest way to do this is to set a flag indicating your updating, and not propagate the update if it's set.

Here's a proof-of-concept example that binds two dividers so the portion between them is always 1/3 of the split pane:

import java.util.List;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.control.SplitPane.Divider;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;

public class SplitPaneDemo extends Application {

    // helper class that binds two divider positions so the portion between them
    // is always 1/3 of the split pane
    private static class DividerPositionBinder {


        private static final double ONE_THIRD = 1.0/3.0;
        private boolean updating ;

        DividerPositionBinder(List<Divider> dividers) {

            dividers.get(0).positionProperty().addListener((obs, oldPos, newPos) -> {
                // don't propagate update if already in an update:
                if (updating) return ;
                // special handling for right edge of split pane:
                if (newPos.doubleValue() > 1.0 - ONE_THIRD) {
                    dividers.get(0).setPosition(1.0 - ONE_THIRD);
                    dividers.get(1).setPosition(1.0);
                    return ;
                }
                // make right divider the new value + 1/3:
                updating = true ;
                dividers.get(1).setPosition(newPos.doubleValue() + ONE_THIRD);
                updating = false ;
            });

            dividers.get(1).positionProperty().addListener((obs, oldPos, newPos) -> {
                // don't propagate update if already in an update:
                if (updating) return ;
                // special handling for left edge of split pane:
                if (newPos.doubleValue() < ONE_THIRD) {
                    dividers.get(1).setPosition(ONE_THIRD);
                    dividers.get(0).setPosition(0.0);
                    return ;
                }
                // make left divider the new value - 1/3:
                updating = true ;
                dividers.get(0).setPosition(newPos.doubleValue() - ONE_THIRD);
                updating = false ;
            });

        }
    }

    @Override
    public void start(Stage primaryStage) {
        Region left = new Pane();
        left.setStyle("-fx-background-color: coral; ");

        Region middle = new Pane();
        middle.setStyle("-fx-background-color: aquamarine ;");

        Region right = new Pane();
        right.setStyle("-fx-background-color: cornflowerblue ;");

        SplitPane splitPane = new SplitPane(left, middle, right);

        new DividerPositionBinder(splitPane.getDividers());


        Scene scene = new Scene(splitPane, 800, 800);
        primaryStage.setScene(scene);
        primaryStage.show();
    }



    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

James_D
  • 201,275
  • 16
  • 291
  • 322