How to access the Scrollbars of a ScrollPane

后端 未结 3 1725
旧巷少年郎
旧巷少年郎 2021-01-01 01:02

I\'m trying to get some information about the ScrollBar components that are by standard included in a ScrollPane. Especially i\'m interested in rea

3条回答
  •  有刺的猬
    2021-01-01 01:16

    I think you can use the lookupAll() method of the Node class for find the scroll bars. http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#lookupAll(java.lang.String)

    For example:

    package com.test;
    
    import java.util.Set;
    import javafx.application.Application;
    import javafx.geometry.Orientation;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.ScrollBar;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.control.ScrollPaneBuilder;
    import javafx.scene.text.Text;
    import javafx.scene.text.TextBuilder;
    import javafx.stage.Stage;
    
    public class JavaFxScrollPaneTest extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
            String longString = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.";
            Text longText = TextBuilder.create().text(longString).build();
    
            ScrollPane scrollPane = ScrollPaneBuilder.create().content(longText).build();
            primaryStage.setScene(new Scene(scrollPane, 400, 100));
            primaryStage.show();
    
            Set nodes = scrollPane.lookupAll(".scroll-bar");
            for (final Node node : nodes) {
                if (node instanceof ScrollBar) {
                    ScrollBar sb = (ScrollBar) node;
                    if (sb.getOrientation() == Orientation.HORIZONTAL) {
                        System.out.println("horizontal scrollbar visible = " + sb.isVisible());
                        System.out.println("width = " + sb.getWidth());
                        System.out.println("height = " + sb.getHeight());
                    }
                }
            }
        }
    }
    

提交回复
热议问题