JavaFX is possible to enable scrolling with hidden

2019-09-01 16:09发布

I am creating a simple VBox wrapped into a ScrollPane. I tried something like:

Into CSS

.scroll-bar{ 
    visibility:hidden;
}

Into the controller:

Set<Node> nodes = scroller.lookupAll(".scroll-bar");
for (final Node node : nodes) {
    if (node instanceof ScrollBar) {
        ScrollBar sb = (ScrollBar) node;
        sb.setVisible(false);
    }
}

I tried also to style the scrollbar in other ways (also assign the class "edge-to-edge") but when the scrollbar is not needed (as default policy) I can see the space that should be occupied by the scrollbar. My goal is to maintain the VBox scrollable, but I want to completely hide the scrollbar.

1条回答
别忘想泡老子
2楼-- · 2019-09-01 16:39

Set the vbarPolicy to NEVER.

@Override
public void start(Stage primaryStage) {
    VBox vbox = new VBox();
    for (int i = 0; i < 40; i++) {
        vbox.getChildren().add(new Text(Integer.toString(i)));
    }

    ScrollPane scrollPane = new ScrollPane(vbox);
    scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); // never show a vertical ScrollBar
    scrollPane.setFitToWidth(true); // set content width to viewport width
    scrollPane.setPannable(true); // allow scrolling via mouse dragging

    primaryStage.setScene(new Scene(scrollPane, 300, 200));
    primaryStage.show();
}
查看更多
登录 后发表回答