问题
I want to access the ScrollBar within a ScrollPane to find the values and do some operations such as increment on click and some other stuff. I cannot find any way of accessing the horizontal and vertical scroll bars. I have tried the solutions to this link but I had no success:
How to access the Scrollbars of a ScrollPane
回答1:
Your problem is that the solution in your link works only after you added the ScrollPane to the stage.
Try this:
ScrollPane scrollPane = new ScrollPane();
primaryStage.setScene(new Scene(scrollPane, 300, 250));
primaryStage.show();
System.out.println("Lookup:");
Set<Node> nodes = scrollPane.lookupAll(".scroll-bar");
for( Node node: nodes) {
System.out.println( node);
}
System.out.println("\nChild Iterator:");
for( Node node: scrollPane.getChildrenUnmodifiable()) {
System.out.println(node);
}
It should give you this:
Lookup:
ScrollBar@8e5ebc5[styleClass=scroll-bar]
ScrollBar@60e1e624[styleClass=scroll-bar]
Child Iterator:
ScrollPaneSkin$3@9a6ff7c[styleClass=viewport]
ScrollBar@8e5ebc5[styleClass=scroll-bar]
ScrollBar@60e1e624[styleClass=scroll-bar]
StackPane@697c10e[styleClass=corner]
In other words: You could either use the lookupAll method of which I still have no idea about whether it'll hold throught the entire existence of JavaFX or not. Or you could iterate throught the children and use the instanceof Operator to find what you need.
来源:https://stackoverflow.com/questions/28904312/how-to-access-scrollbar-in-a-scrollpane-javafx