0

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
}  
Alpha_33
  • 97
  • 8
  • How do you register the instance of `MyClass` ? Are you saying that you are calling `view.getCanvasComponent().addMouseWheelListener(new MyClass())` and your `mouseWheelMoved` only then does not get called, when you *additionally* register the default listener? – Sebastian Nov 26 '20 at 16:47
  • @Sebastian Thanks. myClass has a `main` function. In this main function i create an instance of myClass, in this instance i create a JFrame, initialize all my data, .... and at the END, i register my listeners by calling `registerViewListeners()`. `wheelListener.addToCanvas(view)` take all MouseWheel_Events and `mouseWheelMoved ()` function of MyClass is no longer notified – Alpha_33 Nov 26 '20 at 16:55

1 Answers1

1

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.

Sebastian
  • 7,729
  • 2
  • 37
  • 69
  • it work, thanks. can you explain me please why this work ? now i must add this in my main function (MyClassInstance is an instance of myClass) `MyClassInstance.view.getCanvasComponent().addMouseWheelListener(MyClassInstance);` – Alpha_33 Nov 26 '20 at 17:11
  • I don't understand what kind of explanation you are looking for: If you don't register your listener, how should it ever be called? This is unrelated to yFiles. If you don't add a button click listener to a button, it won't be called, either. – Sebastian Nov 27 '20 at 11:27
  • yes i understend, thanks, but why when i dont call `wheelListener.addToCanvas(view);` the function `mouseWheelMoved` of myClass is well notified ? – Alpha_33 Nov 27 '20 at 11:55
  • I simply don't believe that this would work, either. Rather I guess you had the commented out lines in place, which do exactly the same what I was proposing :-) – Sebastian Nov 27 '20 at 12:25