JavaFX scrollpane – check the displayed components
•
Java
I want to know if there is a scrollpane attribute in JavaFX 8 that can be used to listen for components currently displayed at a given time
Solution
You can check whether the node is visible:
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; }
This method returns a list of all visible nodes All it does is compare the scene coordinates of scrollpane and its children
If you want to create your own observablelist in the property:
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)); }
For complete code, see bitbucket
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码