JavaFX ScrollPane - 检查显示的组件 [英] JavaFX ScrollPane - Check which components are displayed
问题描述
我想知道JavaFX 8中是否有ScrollPane属性可用于侦听当前在给定时间显示的组件。
例如,
ScrollPane有一个VBox,它有8个按钮。滚动窗格中只能看到4个按钮。我想要一个监听器,它会在滚动位置发生变化时显示8个按钮中的4个。
I wonder whether there is a ScrollPane property in JavaFX 8 that can be used to listen on the components that are currently displayed at a given time. For example, ScrollPane has a VBox which has 8 buttons. Only 4 buttons can be seen in scrollpane. I would like a listener that gives those 4 out of 8 buttons that are displayed while the position of the scroll changes.
推荐答案
你可以检查节点是否可见:
You can check if the Nodes visible like that:
private List<Node> getVisibleNodes(ScrollPane pane) {
List<Node> visibleNodes = new ArrayList<>();
Bounds paneBounds = pane.localToScene(pane.getBoundsInParent());
if (pane.getContent() instanceof Parent) {
for (Node n : ((Parent) pane.getContent()).getChildrenUnmodifiable()) {
Bounds nodeBounds = n.localToScene(n.getBoundsInLocal());
if (paneBounds.intersects(nodeBounds)) {
visibleNodes.add(n);
}
}
}
return visibleNodes;
}
此方法返回所有可见节点的列表。
它只是比较ScrollPane及其子节点的场景坐标。
This method returns a List of all Visible Nodes. All it does is compare the Scene Coordinates of the ScrollPane and its Children.
如果你想在属性中创建你自己的ObservableList:
If you want them in a Property just create your own ObservableList:
private ObservableList<Node> visibleNodes;
...
visibleNodes = FXCollections.observableArrayList();
ScrollPane pane = new ScrollPane();
pane.vvalueProperty().addListener((obs) -> {
checkVisible(pane);
});
pane.hvalueProperty().addListener((obs) -> {
checkVisible(pane);
});
private void checkVisible(ScrollPane pane) {
visibleNodes.setAll(getVisibleNodes(pane));
}
有关完整代码,请参阅 BitBucket
For full Code see BitBucket
这篇关于JavaFX ScrollPane - 检查显示的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!