SplitPane 分隔线同步 JavaFX

SplitPane dividers synchronization JavaFX

我想同步SplitPane中的分隔线,当分隔线(0)移动时,我也想通过分隔线(1)做同样的移动。我想我必须用一些东西绑定 divider(0) 的 positionProperty。

我怎样才能做到这一点?

您需要在每个分频器的位置添加监听器,并在"linked"分频器发生变化时更新。确保您不会陷入无限递归循环很重要;执行此操作的最简单方法是设置一个指示更新的标志,如果已设置,则不传播更新。

这是一个 proof-of-concept 示例,它绑定了两个分隔线,因此它们之间的部分始终是拆分窗格的 1/3:

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