How to disable horizontal scrolling in ScrollBar (JavaFX)?
Asked Answered
A

3

15

I disabled showing the horizontal ScrollBar with following code:

scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

It's no longer visible, but usuable with the mouse wheel. How can I prevent that?

There's a ScrollPane with vertical ScrollBar. But I can click the mouse wheel to right and the content is scrolling horizontal.

Thanks.

Azaleeazan answered 22/5, 2015 at 7:39 Comment(2)
Can you post a MCVE ?Americana
Do you want the scrollpane height to fit its content height ?Quelpart
Q
3

EDIT: Use this answer instead of this hacky one


You could consume the horizontal ScrollEvent in an event filter:

    scrollPane.addEventFilter(ScrollEvent.SCROLL,new EventHandler<ScrollEvent>() {
        @Override
        public void handle(ScrollEvent event) {
            if (event.getDeltaX() != 0) {
                event.consume();
            }
        }
    });
Quelpart answered 22/5, 2015 at 8:45 Comment(1)
this is such an ugly hack, i cant even! It has so many unwanted sideeffects.. please use the solution further below using setFitToWidth(true)!Assassin
P
49

scrollPane.setFitToWidth(true);

Piccolo answered 22/5, 2015 at 8:46 Comment(1)
how do i apply this method to listviwe or jfxlistviwe?Pallor
T
6

I believe this is the proper way:

sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
Tegular answered 23/8, 2016 at 0:2 Comment(0)
Q
3

EDIT: Use this answer instead of this hacky one


You could consume the horizontal ScrollEvent in an event filter:

    scrollPane.addEventFilter(ScrollEvent.SCROLL,new EventHandler<ScrollEvent>() {
        @Override
        public void handle(ScrollEvent event) {
            if (event.getDeltaX() != 0) {
                event.consume();
            }
        }
    });
Quelpart answered 22/5, 2015 at 8:45 Comment(1)
this is such an ugly hack, i cant even! It has so many unwanted sideeffects.. please use the solution further below using setFitToWidth(true)!Assassin

© 2022 - 2024 — McMap. All rights reserved.