views:

67

answers:

4

How can I generate event MouseWheelEvent for some Object?

+3  A: 

You can use Robot class.Take a look at Mouse Wheel method

mcaaltuntas
+2  A: 

Well, I don't know about generating a scroll Event from an object, but you should take a look at the Robot class. It allows you to actually cause the mouse wheel to scroll. That may be helpful.

Specifically, look at the mouseWheel(int) method.

jjnguy
+1  A: 

Do you want to actually fire a MouseWheelEvent, or do you want to do something when a mouse wheel is scrolled?

If it is the former you can create a new instance using the constructor:

new MouseWheelEvent(Component source, int id, long when, int modifiers, int x, 
    int y,  int clickCount, boolean popupTrigger, int scrollType, 
    int scrollAmount, int wheelRotation) 

and pass the instance to the component's processMouseWheelEvent method to fire it.

If it's the latter, you can either implement a MouseWheelListener and add it to your component using the addMouseWheelListener() method of Component.

Jeff
I want to fire a MouseWheelEvent :)
subSeven
I've run into some weird issues with creating mouse events and having them properly handled. If you know exactly the component to hand the event to, this method will work fine. Otherwise, I would suggest using a `Robot` as mentioned above. (And if you really are just handing off a `MouseWheelEvent` to one of your components, it sounds like a little refactoring might be in order. Pull the mouse wheel logic out of that method so that you can instead call `handleMouseWheel(int ticks)` from elsewhere without having to construct an `Event` just to pass the info.
Mike
+2  A: 
MouseWheelEvent mwe = new MouseWheelEvent(...);
component.dispatchEvent( mwe );
camickr