Synchronizing two scroll bars JavaFX

前端 未结 1 1007
死守一世寂寞
死守一世寂寞 2020-12-12 01:46

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.valuePr

相关标签:
1条回答
  • 2020-12-12 02:31

    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);
        });
    }
    
    0 讨论(0)
提交回复
热议问题