How to access ScrollBar in a ScrollPane? (JavaFX)

纵然是瞬间 提交于 2019-12-08 13:22:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!