有什么方法可以同步 JavaFX 1.2 ListView 中的滚动条吗?

发布于 2024-08-31 00:06:56 字数 133 浏览 4 评论 0原文

我并排有多个列表视图。我有办法确保“selectedIndex”在所有这些上都相同,但是有没有办法使滚动条始终同步?

似乎只要用户在向下导航列表视图时使用箭头键,滚动条就会同步,但是,如果用户按住该键或使用鼠标滚轮,则它们根本不会同步。

I have multiple listviews sidebyside. I have a way to make sure the "selectedIndex" is the same on all of them, but is there a way to make it so the scrollbar's are ALWAYS synchronized?

It seems that the scrollbars WILL be synced as long as the user uses the ARROW KEYS when navigating down the listview, however, if the user HOLDS DOWN the key, OR USES THE MOUSE WHEEL, they will not be synchronized at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-09-07 00:06:56

不幸的是,这是不可能的,即使在 1.3 中也无法访问内部滚动条,并且也有任何方法可以设置自定义滚动条。如果可以的话,请针对 JavaFX 运行时提出问题,描述您的用例。 http://javafx-jira.kenai.com

Unfortunately this is not possible, even in 1.3 there is no access to internal scrollbar and also there is any way how to set up custom scrollbar. If you can please file an issue against JavaFX Runtime describing your use-case. http://javafx-jira.kenai.com

遇到 2024-09-07 00:06:56

老问题,但万一有人感兴趣:我现在使用下面的类(在 FX2.2 中测试)来同步 2 个 ListView 的滚动:

package fx.scrollbars;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollBar;

/**
 * Created by JRD on 03/03/2015.
 */
public class ScrollbarsBinding {

public static final int BIND_BIDIRECTIONAL = 3;
public static final int BIND_RIGHT_TO_LEFT = 1;
public static final int BIND_LEFT_TO_RIGHT = 2;

/**
 * Bidirectional binding of 2 ListView's scrollbars.
 */
public static void bind(ListView lv1, ListView lv2) {
    bind(lv1, lv2, BIND_BIDIRECTIONAL);
}

/**
 * Allows binding of 2 ListView's scrollbars.
 * @param lv1 left list view
 * @param lv2 right list view
 * @param bindType 3 binding possibilities : BIND_BIDIRECTIONAL=bidirectional, BIND_RIGHT_TO_LEFT and BIND_LEFT_TO_RIGHT
 */
public static void bind(ListView lv1, ListView lv2, int bindType) {
    ScrollBar bar1 = null;
    ScrollBar bar2 = null;

    for (Node node : lv1.lookupAll(".scroll-bar")) {
        if (node instanceof ScrollBar && ((ScrollBar)node).getOrientation().equals(Orientation.VERTICAL)) {
            bar1 = (ScrollBar)node;
        }
    }
    for (Node node : lv2.lookupAll(".scroll-bar")) {
        if (node instanceof ScrollBar && ((ScrollBar)node).getOrientation().equals(Orientation.VERTICAL)) {
            bar2 = (ScrollBar)node;
        }
    }
    if (bar1 == null || bar2 == null) return;

    final ScrollBar fbar1 = bar1;
    final ScrollBar fbar2 = bar2;
    if (fbar1 != null && (bindType & BIND_RIGHT_TO_LEFT) != 0) {
        fbar1.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                fbar2.setValue(newValue.doubleValue());
            }
        });
    }
    if (fbar2 != null && (bindType & BIND_LEFT_TO_RIGHT) != 0) {
        fbar2.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                fbar1.setValue(newValue.doubleValue());
            }
        });
    }
}

// TODO: other JavaFx components ?
}

然后我只需调用以下代码:

ScrollbarsBinding.bind(listView1, listView2);

Old question but in case somebody is interested : I now use the class below (Tested in FX2.2) to synchronize the scrolling of 2 ListViews :

package fx.scrollbars;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollBar;

/**
 * Created by JRD on 03/03/2015.
 */
public class ScrollbarsBinding {

public static final int BIND_BIDIRECTIONAL = 3;
public static final int BIND_RIGHT_TO_LEFT = 1;
public static final int BIND_LEFT_TO_RIGHT = 2;

/**
 * Bidirectional binding of 2 ListView's scrollbars.
 */
public static void bind(ListView lv1, ListView lv2) {
    bind(lv1, lv2, BIND_BIDIRECTIONAL);
}

/**
 * Allows binding of 2 ListView's scrollbars.
 * @param lv1 left list view
 * @param lv2 right list view
 * @param bindType 3 binding possibilities : BIND_BIDIRECTIONAL=bidirectional, BIND_RIGHT_TO_LEFT and BIND_LEFT_TO_RIGHT
 */
public static void bind(ListView lv1, ListView lv2, int bindType) {
    ScrollBar bar1 = null;
    ScrollBar bar2 = null;

    for (Node node : lv1.lookupAll(".scroll-bar")) {
        if (node instanceof ScrollBar && ((ScrollBar)node).getOrientation().equals(Orientation.VERTICAL)) {
            bar1 = (ScrollBar)node;
        }
    }
    for (Node node : lv2.lookupAll(".scroll-bar")) {
        if (node instanceof ScrollBar && ((ScrollBar)node).getOrientation().equals(Orientation.VERTICAL)) {
            bar2 = (ScrollBar)node;
        }
    }
    if (bar1 == null || bar2 == null) return;

    final ScrollBar fbar1 = bar1;
    final ScrollBar fbar2 = bar2;
    if (fbar1 != null && (bindType & BIND_RIGHT_TO_LEFT) != 0) {
        fbar1.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                fbar2.setValue(newValue.doubleValue());
            }
        });
    }
    if (fbar2 != null && (bindType & BIND_LEFT_TO_RIGHT) != 0) {
        fbar2.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                fbar1.setValue(newValue.doubleValue());
            }
        });
    }
}

// TODO: other JavaFx components ?
}

Then I just have to call this code :

ScrollbarsBinding.bind(listView1, listView2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文