同步两个滚动条JavaFX

时间:2017-10-04 11:14:15

标签: javafx scrollbar

我的问题是我有两个水平滚动条,我希望它们齐声滚动并且我尝试过使用:

bar1.valueProperty().bindBidirectional(bar2.valueProperty());

问题在于,我注意到bar1的最大值为1.0,而bar2的最大值为102.5。因此,当我滚动bar2bar1由于其价值的巨大差异而移动很多时,问题就出现了。在绑定value属性之前,我尝试使用setMinsetMaxsetUnitIncrementsetBlockIncrement。但是没有用。

public class DynamicTableView extends Application {
    private Scene scene;
    private VBox root;
    private ScrollPane scPane;
    private static final int N_COLS = 10;
    private static final int N_ROWS = 10;
    private Button button;

    public void start(Stage stage) throws Exception {

        root = new VBox();
        scPane = new ScrollPane();
        Label lbl = new Label("Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table");
        scPane.setContent(lbl);

        root.getChildren().add(scPane);

        TestDataGenerator dataGenerator = new TestDataGenerator();

        TableView<ObservableList<String>> tableView = new TableView<>();

        // add columns
        List<String> columnNames = dataGenerator.getNext(N_COLS);
        for (int i = 0; i < columnNames.size(); i++) {
            final int finalIdx = i;
            TableColumn<ObservableList<String>, String> column = new TableColumn<>(
                    columnNames.get(i)
            );
            column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx)));
            tableView.getColumns().add(column);
        }

        // add data
        for (int i = 0; i < N_ROWS; i++) {
            tableView.getItems().add(
                    FXCollections.observableArrayList(
                            dataGenerator.getNext(N_COLS)
                    )
            );
        }

        root.getChildren().add(tableView);

        tableView.setPrefHeight(200);

        button = new Button("Delete");
        button.setOnAction(event -> { tableView.getItems().clear();});

        root.getChildren().add(button);

        Scene scene = new Scene(root, 600, 600);
        stage.setScene(scene);
        stage.show();

        for(Node node1: scPane.lookupAll(".scroll-bar"))
        {
            if(node1 instanceof ScrollBar)
            {
                ScrollBar scrollBar1 = (ScrollBar)node1;
                if(scrollBar1.getOrientation() == Orientation.HORIZONTAL)
                {
                    for(Node node2: tableView.lookupAll(".scroll-bar"))
                    {
                        if(node2 instanceof ScrollBar)
                        {
                            ScrollBar scrollBar2 = (ScrollBar)node2;
                            if(scrollBar2.getOrientation() == Orientation.HORIZONTAL)
                            {
                                scrollBar2.valueProperty().bindBidirectional(scrollBar1.valueProperty());
                                break;
                            }
                        }
                    }
                }
            }
        }

    }

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

    private static class TestDataGenerator {
        private static final String[] LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tempus cursus diam ac blandit. Ut ultrices lacus et mattis laoreet. Morbi vehicula tincidunt eros lobortis varius. Nam quis tortor commodo, vehicula ante vitae, sagittis enim. Vivamus mollis placerat leo non pellentesque. Nam blandit, odio quis facilisis posuere, mauris elit tincidunt ante, ut eleifend augue neque dictum diam. Curabitur sed lacus eget dolor laoreet cursus ut cursus elit. Phasellus quis interdum lorem, eget efficitur enim. Curabitur commodo, est ut scelerisque aliquet, urna velit tincidunt massa, tristique varius mi neque et velit. In condimentum quis nisi et ultricies. Nunc posuere felis a velit dictum suscipit ac non nisl. Pellentesque eleifend, purus vel consequat facilisis, sapien lacus rutrum eros, quis finibus lacus magna eget est. Nullam eros nisl, sodales et luctus at, lobortis at sem.".split(" ");

        private int curWord = 0;

        List<String> getNext(int nWords) {
            List<String> words = new ArrayList<>();

            for (int i = 0; i < nWords; i++) {
                if (curWord == Integer.MAX_VALUE) {
                    curWord = 0;
                }

                words.add(LOREM[curWord % LOREM.length]);
                curWord++;
            }

            return words;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Bidi-将scrollPane中scrollBar的valueProperty绑定到tableView中scrollBar的值,因为前者是规范化的,而后者不是。相反,请手动收听更改和缩放,例如:

protected void bindScrollBarValues(ScrollBar scrollBarInTable, ScrollBar scrollBarInPane) {
    // can't use bidi-binding because bar in scrollPane is normalized, bar in table is not
    // scrollBarInTable.valueProperty().bindBidirectional(scrollBarInPane.valueProperty());
    // scale manually
    scrollBarInTable.valueProperty().addListener((src, ov, nv) -> {
        double tableMax = scrollBarInTable.getMax();
        scrollBarInPane.setValue(nv.doubleValue() / tableMax);
    });

    scrollBarInPane.valueProperty().addListener((src, ov, nv) -> {
        double tableMax = scrollBarInTable.getMax();
        scrollBarInTable.setValue(nv.doubleValue() * tableMax);
    });
}