Class ScrollEvent

java.lang.Object
All Implemented Interfaces:
Serializable, Cloneable

public final class ScrollEvent extends GestureEvent
Scroll event indicates that user performed scrolling by mouse wheel, track pad, touch screen or other similar device.

When the scrolling is produced by a touch gesture (such as dragging a finger over a touch screen), it is surrounded by the SCROLL_STARTED and SCROLL_FINISHED events. Changing number of involved touch points during the scrolling is considered a new gesture, so the pair of SCROLL_FINISHED and SCROLL_STARTED notifications is delivered each time the touchCount changes. When the scrolling is caused by a mouse wheel rotation, only a one-time SCROLL event is delivered, without the started/finished surroundings. If scrolling inertia is active on the given platform, some SCROLL events with isInertia() returning true can come after SCROLL_FINISHED.

The event is delivered to the top-most node picked on the gesture coordinates in time of the gesture start - the whole gesture is delivered to the same node even if the coordinates change during the gesture. For mouse wheel rotation the event is delivered to the top-most node picked on mouse cursor location. The delivery is independent of current focus owner.

The event provides two different types of scrolling values: pixel-based and character/line-based. The basic deltaX and deltaY values give reasonable results when used as number of pixels to scroll (The totalDeltaX and totalDeltaY contain the cumulative values for the whole gesture, zeros for mouse wheel). For scrolling text (or other line-based content as tables) the textDelta values should be used if they are available. The textDeltaXUnits and textDeltaYUnits determine how to interpret the textDeltaX and textDeltaY values. If the units are set to NONE, the text-based values are not available (not provided by the underlying platform) and the pixel-based values need to be used.

As all gestures, scrolling can be direct (performed directly at the concrete coordinates as on touch screen - the center point among all the touches is usually used as the gesture coordinates) or indirect (performed indirectly as on track pad or with mouse - the mouse cursor location is usually used as the gesture coordinates).

For example, scrolling a graphical node can be achieved by following code:


    node.setOnScroll(new EventHandler<ScrollEvent>() {
        @Override public void handle(ScrollEvent event) {
            node.setTranslateX(node.getTranslateX() + event.getDeltaX());
            node.setTranslateY(node.getTranslateY() + event.getDeltaY());
        }
    });

A scroll event handler on text-based component behaving according to system settings on all platforms should contain following logic:


    switch(event.getTextDeltaYUnits()) {
        case LINES:
            // scroll about event.getTextDeltaY() lines
            break;
        case PAGES:
            // scroll about event.getTextDeltaY() pages
            break;
        case NONE:
            // scroll about event.getDeltaY() pixels
            break;
    }
 
Since:
JavaFX 2.0
See Also: