简体   繁体   中英

JavaFX scrollpane - horizontal panning with scroll wheel

I can trivially create a scroll pane in JavaFX that only scrolls horizontally like so:

ScrollPane scroll = new ScrollPane(lcp);
scroll.setPannable(true);
scroll.setFitToHeight(true);
scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

However, the mouse scroll wheel still tries to scroll vertically rather than horizontally in this case (unless I specifically scroll over the horizontal scroll bar.)

How can I set up the scroll pane so that the mouse wheel pans horizontally?

Here is the example application that I wrote for you and does exactly what you want:

public class Test extends Application {

    ScrollPane scrollPane;
    int pos = 0;
    final int minPos = 0;
    final int maxPos = 100;

    @Override
    public void start(Stage primaryStage) {

        Label label = new Label("TEXT!!!!!!!TEXT!!!!!!!TEXT!!!!!!!TEXT!!!!!!!TEXT!!!!!!!TEXT");
        label.setPrefSize(500, 100);
        label.setOnScroll(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent event) {

                if (event.getDeltaY() > 0)
                    scrollPane.setHvalue(pos == minPos ? minPos : pos--);
                else
                    scrollPane.setHvalue(pos == maxPos ? maxPos : pos++);

            }
        });

        scrollPane = new ScrollPane();
        scrollPane.setHmin(minPos);
        scrollPane.setHmax(maxPos);
        scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);
        scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        scrollPane.setPannable(true);
        scrollPane.setFitToHeight(true);
        scrollPane.setContent(label);

        BorderPane root = new BorderPane();
        root.setPrefSize(200, 100);
        root.setCenter(scrollPane);

        primaryStage.setScene(new Scene(root));
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

I have also been looking for a solution and found this one from Ugurcan Yildirim but didn't like the fact that the natural scroll bar length and speed is modified also. This one worked for me:

scrollPane.setOnScroll(event -> {
    if(event.getDeltaX() == 0 && event.getDeltaY() != 0) {
        scrollPane.setHvalue(scrollPane.getHvalue() - event.getDeltaY() / this.allComments.getWidth());
    }
});

event.getDeltaX() == 0 just to be sure that the user is only using the mouse wheel and nothing is adding up

this.allComments is the content of the scrollPane (a HBox in my case). By dividing the delta y value by it's content width the scroll speed is natural according to the amount of content to scroll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM