Search code examples
javaswingmousewheelyfiles

How to catch/forward MouseWheel event to several objects?


I am using yFiles library and I am handling a Graph2DView object named view. To activate mouseWheel scroll for this object, I have to add a listener in registerViewListeners function. However, I also want to be notified also in myClass mouseWheelMoved function when mouseweel

public class MyClass extends MyBaseClass implements MouseWheelListener {
    
    Graph2DView view;  
    // ..... 

    @Override
    protected void registerViewListeners()   
    {  
         Graph2DViewMouseWheelScrollListener wheelListener = new Graph2DViewMouseWheelScrollListener();
         wheelListener.addToCanvas(view);
    
         // The two precedent instruction is equivalent to 
         // view.getCanvasComponent().addMouseWheelListener(this);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) 
    {    
        // some work ...
    }  
}  

Problem:
If i register my view object through registerViewListeners

 @Override
 protected void registerViewListeners()   
 {  
    Graph2DViewMouseWheelScrollListener wheelListener = new Graph2DViewMouseWheelScrollListener();
    wheelListener.addToCanvas(view);
 }

My mouseWheelMoved function is no longer notified:

@Override
public void mouseWheelMoved(MouseWheelEvent e) 
{    
    // not called
}  

Solution

  • Your description sounds as if you have simply removed the registration for your event listener.

    In the place where you instantiate MyClass, please also add:

     view.getCanvasComponent().addMouseWheelListener(myClassInstance); // register listener
    

    If you don't register your listener, it will not be called, of course. Only instantiating it will not suffice.