Mike Nakis
Mike Nakis

Reputation: 61969

Java Swing MouseWheelEvent WHEEL_BLOCK_SCROLL

The question:

In Swing, when my MouseWheelListener receives a MouseWheelEvent, how can I combine the multitude of parameters contained within that event to obtain a single signed integer representing the number of units to scroll by, taking into account the fact that the "event type" may be either MouseWheelEvent.WHEEL_UNIT_SCROLL or MouseWheelEvent.WHEEL_BLOCK_SCROLL ?

Background:

Anyone who has ever had to deal with Swing's MouseWheelEvent must have noticed that it is preposterously complicated: instead of a single parameter containing the amount by which to scroll, it offers 5 parameters: getScrollType(), getScrollAmount(), getWheelRotation(), getPreciseWheelRotation(), and getUnitsToScroll().

The "scroll type" appears to be crucial for this, however the documentation (Which nowadays can be found at Oracle - Class MouseWheelEvent) mentions but does not explain the value of WHEEL_BLOCK_SCROLL.

Adding insult to injury, Oracle's own "tutorial" on the mouse wheel listener Oracle: How to Write a Mouse-Wheel Listener completely fails to show what to do when WHEEL_BLOCK_SCROLL is received, and instead shows how to display "WHEEL_BLOCK_SCROLL" when that happens.

It appears that WHEEL_BLOCK_SCROLL is very seldom implemented, so it is quite unclear how to handle it, and how to even observe it happening. One of the very rare pieces of code that you can find out there which attempts to do something meaningful with WHEEL_BLOCK_SCROLL is at Github / iconfinder / batik / sources / org / apache / batik / swing / JSVGScrollPane.java but the code is commented-out, so I cannot trust that it ever worked.

Perhaps events of type WHEEL_BLOCK_SCROLL can be triggered if you go to "Mouse" settings in Windows, and in the "Roll the mouse wheel to scroll" box select "One screen at a time" instead of "Multiple lines at a time", but I have not tried this yet. Here is an unanswered question about this on stackoverflow: How do I generate a java swing WHEEL_BLOCK_SCROLL event?

Upvotes: 0

Views: 199

Answers (1)

queeg
queeg

Reputation: 9384

Just by looking at the documentation, and not having worked with MouseWheelEvents in practice, I suspect that the method getUnitsToScroll() performs exactly what you are asking for.

The documentation says:

This is a convenience method to aid in the implementation of the common-case MouseWheelListener - to scroll a ScrollPane or JScrollPane by an amount which conforms to the platform settings. (Note, however, that ScrollPane and JScrollPane already have this functionality built in.)

This method returns the number of units to scroll when scroll type is MouseWheelEvent.WHEEL_UNIT_SCROLL, and should only be called if getScrollType returns MouseWheelEvent.WHEEL_UNIT_SCROLL.

In case you hit a WHEEL_BLOCK_SCROLL, probably the page-up or page-down keys were pressed. In that case the amount to scroll is one page size, resembling the component's getHeight() value.

Upvotes: 1

Related Questions