Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronizing two scroll bars JavaFX

My problem is I have two horizontal scroll bars that I want them to scroll in unison and I've tried using:

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

The problem is that i notice bar1's max value is 1.0 while bar2's max value is 102.5. Hence, the problem, when i scroll bar2, bar1 moves a lot due to the large discrepancy in their value. I've tried using setMin, setMax, setUnitIncrement, setBlockIncrement before binding the value property. But is doesn't work.

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;
        }
    }
}
like image 931
ekxz900 Avatar asked May 27 '26 14:05

ekxz900


1 Answers

Bidi-Binding of valueProperty of a scrollBar in a scrollPane to that of a scrollBar in a tableView is not possible because the former is normalized while the latter is not. Instead, listen to changes and scale manually, like:

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);
    });
}
like image 157
kleopatra Avatar answered Jun 01 '26 06:06

kleopatra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!