Does anayone know how to disable scrolling by Mousewheel in a ScrollPane?
Asked
Active
Viewed 3,181 times
2 Answers
3
The following works for me:
scrollPane.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
@Override
public void handle(ScrollEvent event) {
if (event.getDeltaY() > 0) {
zoomIn();
} else {
zoomOut();
}
event.consume();
}});
You may find that you also need something like the following:
scrollPane.setOnScroll(new EventHandler<ScrollEvent>() {
@Override
public void handle(ScrollEvent event) {
if (event.getDeltaY() > 0) {
zoomIn();
} else {
zoomOut();
}
event.consume();
}
});
I added the above elaboration to another answer in this thread, but it hasn't shown up in the public feed from what I can tell. So, I've pasted it in its own answer.
This question is a bit of a duplicate, but it showed up in Google for me first, so I'm answering it. The inspiration for the above is:
Zooming in JavaFx: ScrollEvent is consumed when content size exceeds ScrollPane viewport

Community
- 1
- 1

interestedparty333
- 2,386
- 1
- 21
- 35
2
I think there is no direct solution.
So I would add an event filter to the ScrollPane for the SCROLL EventType and consume every event. That should prevent any mouse generated scroll events from being delegated to the ScrollPane.

eckig
- 10,964
- 4
- 38
- 52
-
Ok, thanks. This helps me for now. But what if I want to enable scrolling on an element within the scrollpane. For example a TableView? – Studiosus Dec 28 '14 at 15:00
-
You can check the event target of the `ScrollEvent` and only consume it, if it matches your criteria. – eckig Dec 28 '14 at 15:14
-
I tried it, but the event target is every time the scrollpane. :( – Studiosus Dec 28 '14 at 15:33
-
When I know how to push panes behind each other, my problem is solved, too. Because my main idea is to slide panes in and out, I tried it with a GridPane within a Stackpane, but when I move the gridpane (this is how I implemented the sliding effect), it's always in front of other views. With a Scrollpane I don't have this problem. – Studiosus Dec 28 '14 at 15:49
-
You can use `toFront()` or `toBack()` to move component z-order. Also you can have a look at the `Pagination` control for sliding in and out components. – eckig Dec 28 '14 at 15:51
-
Ok, now I tried also the toFront() and toBack() methode, but this doesn't work, too. The problem is that the content, which have to be in the front is in the head of a borderPane and the gridPane is in the center of the borderPane. So the z-order does not seem to be relevant for that. – Studiosus Dec 28 '14 at 17:11
-
1Yeeeaaaahh, I solved the problem! The solution is `setClip()` methode. Here is the hole solution: `Rectangle clipRectangle = new Rectangle(); clipRectangle.widthProperty().bind(contentPane.widthProperty()); clipRectangle.heightProperty().bind(contentPane.heightProperty()); contentPane.setClip(clipRectangle);` – Studiosus Dec 28 '14 at 17:38
-
1@Studiosus you should answer on your own question if you found a solution for your answer. This helps other people who are searching for this – NDY Mar 11 '15 at 13:43