如何在ScrollPane中访问ScrollBar? (JavaFX) [英] How to access ScrollBar in a ScrollPane? (JavaFX)

查看:277
本文介绍了如何在ScrollPane中访问ScrollBar? (JavaFX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问ScrollPane中的ScrollBar以查找值并进行一些操作,例如单击增量和其他一些操作.我找不到任何访问水平和垂直滚动条的方法.我已经尝试过此链接的解决方案,但没有成功:

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:

如何访问ScrollPane的滚动条

推荐答案

您的问题是您的链接中的解决方案仅在将ScrollPane添加到场景中之后才有效.

Your problem is that the solution in your link works only after you added the ScrollPane to the stage.

尝试一下:

    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);
    }

它应该给你这个:

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]

换句话说:您可以使用lookupAll方法,但我仍然不知道该方法是否可以在整个JavaFX存在的情况下保持有效.或者,您可以遍历子级并使用instanceof运算符查找所需的内容.

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.

这篇关于如何在ScrollPane中访问ScrollBar? (JavaFX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆