例如,说我们有一个滚动条,用于显示/处理某些应用程序上的数据。滚动条也具有最小值,最大值和可见量。但是,根据滚动条值显示的值范围有些混乱。
作为一个例子,这里是一张图片
和
但是它是根据滚动条的拇指不同侧计算的。
说我需要显示数据库中的一些数据,最小值为min id,最大值也为max id。并考虑我要显示100个项目,并且我要在滚动条中将可见数量设置为100。滚动时如果scroolbar的值等于1,那么从数据库中获取范围就像将100(可见量)加到1一样简单,但是当值等于max时将不起作用。因此,这就是为什么我需要获取滚动条缩略图的值范围的原因,但是在查看滚动条源代码时,我找不到此功能的实现。
如何获取拇指的最大值和最小值或滚动条的可见值范围?
答案 0 :(得分:0)
ScrollBar
表示位置,而不是范围。该位置可通过value
属性获得。 visualAmount
属性确定滑块的大小。
如果value == max
,则拇指位于最右边/最下面的位置。您的“拇指的最大值”将超过max
。
因此,您应根据以下内容决定要显示多少项并计算visibleAmount
和max
:
max = itemCount - displayedItems
visibleAmount = max * displayedItems / itemCount
类似ScrollPane
的实现示例:
@Override
public void start(Stage stage) {
ScrollBar scrollBar = new ScrollBar();
scrollBar.setOrientation(Orientation.VERTICAL);
StackPane.setAlignment(scrollBar, Pos.CENTER_RIGHT);
VBox container = new VBox();
StackPane.setAlignment(container, Pos.TOP_LEFT);
StackPane root = new StackPane(container, scrollBar);
InvalidationListener listener = o -> {
// adjust scrollbar properties on resize of root or content
double rootHeight = root.getHeight();
double contentHeight = container.getHeight();
double max = Math.max(0, contentHeight - rootHeight);
scrollBar.setMax(max);
scrollBar.setVisibleAmount(max * rootHeight / contentHeight);
};
root.heightProperty().addListener(listener);
container.heightProperty().addListener(listener);
// move container up based on the scrollbar value
container.translateYProperty().bind(scrollBar.valueProperty().negate());
// generate some content
for (int i = 0; i < 10; i++) {
Rectangle rect = new Rectangle(100, 100, (i & 1) == 0 ? Color.BLACK : Color.LIGHTGRAY);
container.getChildren().add(rect);
}
Scene scene = new Scene(root, 300, 300);
stage.setScene(scene);
stage.show();
}