How to Move the Scroll Bars Using the Mouse Wheel

Questions & Answers

Summary

Using the mouse wheel to move the scroll bars.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

By default, the yFiles viewer component's support for mouse wheels applies to zooming in and out of the view. To use the mouse wheel for moving the scroll bars a customized MouseWheelListener has to be registered.

Mouse wheel support is provided by yFiles version 2.3 and higher.

The following sample code shows a simple MouseWheelListener implementation that can be registered with a view's JComponent.

class WheelScroller implements MouseWheelListener
{
  protected Graph2DView view;

  public WheelScroller(Graph2DView view)
  {
    this.view = view;
  }

  public void mouseWheelMoved(MouseWheelEvent e)
  {
    Point2D p2 = view.getCenter();
    Point p = view.getViewPoint();
    Dimension d = view.getViewSize();
    Rectangle r = view.getWorldRect();

    if (e.getWheelRotation() >= 0)
    {
      if (r.getY() + r.getHeight() - 1 > p.getY() + d.height / view.getZoom())
        p2.setLocation(p2.getX(), p2.getY() + e.getScrollAmount());
    }
    else
    {
      if (r.getY() + 1 < p.getY())
        p2.setLocation(p2.getX(), p2.getY() - e.getScrollAmount());
    }

    if (r.contains(p2))
    {
      view.setCenter(p2.getX(), p2.getY());
      view.updateView();
    }
  }
}
Since yFiles version 2.10 class Graph2DViewMouseWheelScrollListener supports scrolling in horizontal or vertical direction.

Resources

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > View Implementations
Applies to:
yFiles for Java 2: 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
mouse - wheel - scroll - bars - scrollbar - scrollbars