How to Move the Scroll Bars Using the Mouse Wheel
Applies to: yFiles for Java 2.9, 2.8, 2.7, 2.6, 2.5, 2.4, 2.3 print article email article

Type: Questions & Answers

Using the mouse wheel to move the scroll bars.

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.
Note
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();
    }
  }
}

Keywords: mouse - wheel - scroll - bars - scrollbar - scrollbars

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2012 yWorks · ALL RIGHTS RESERVED imprint | terms of use | privacy policy | home