GLCanvas is blocking mouse wheel events

hi

I’m trying to catch mouse events from the AWT Toolkit. Motion events work great as well as button events. Unfortunately wheel events are blocked by the GLCanvas. Is this a bug or a feature and is there a workaround/fix for it?

Here is my test case:


import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;

import javax.media.opengl.GLCanvas;
import javax.swing.JFrame;

public class AWTMouseWheelTest extends JFrame implements AWTEventListener
{
    private static final long serialVersionUID = 1L;
    
    public void eventDispatched(AWTEvent event)
    {
        System.out.println( event );
    }
    
    public AWTMouseWheelTest()
    {
        super( "sdfsdf" );
        
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        this.setBounds( 100, 100, 640, 480 );
        
        this.getContentPane().setLayout( null );
        
        GLCanvas canvas = new GLCanvas();
        canvas.setBounds( 10, 10, 100, 100 );
        this.getContentPane().add( canvas );
        
        System.out.println( canvas.getMouseWheelListeners().length );
        
        Toolkit.getDefaultToolkit().addAWTEventListener( this, AWTEvent.MOUSE_WHEEL_EVENT_MASK );
        
        this.setVisible( true );
    }
}

Any help is appreciated.

Marvin

I had a similar issue with Java3D eating my events and found that a work around was to call:
canvas.setFocusable(false);
canvas.setEnabled(false);

but still add it to the panel or frame. I’m not sure with which actually stops the canvas from eating the events but it worked for me.
I haven’t tried it with wheel events, but I know it works with keyboard events.

Thanks for the reply :).

Any other events work fine without a workaround. It’s just the wheel, that doesn’t work.

Marvin

I’ve had the similar problem on AWT with the Keyboard. Hence I just changed the approach to listen to the Keyboard: the KeyEventDispatcher used commonly with KeyboardFocusManager, that works fine. but for the mouse I’m not sure about the existance of a “MouseFocusManager”, isn’t it? ???

Take a look at bug 6516675 in Sun’s bug database. It might be related. I think we found workarounds for all of the mouse wheel event related issues we’ve found so far; making the GLCanvas focusable sounds like the right thing to do.

This is a very interesting example. Just to be sure, I modified your code to mask AWTEvent.MOUSE_EVENT_MASK instead of MOUSE_WHEEL_EVENT_MASK and the program works as expected (mouse enter and exit events at the GLCanvas). So there’s nothing wrong with your code. However, as you discovered, the AWTEvent.MOUSE_WHEEL_EVENT_MASK doesn’t seem to enable the mouse wheel events.

Then, if I add a specific call to add a listener (with a no-op handler):


canvas.addMouseWheelListener(new MouseWheelListener()
{
	public void mouseWheelMoved(MouseWheelEvent e) {}
});

Then the mouse wheel events begin to work in the GLCanvas. It looks like some extra magic is performed in addMouseWheelListener() to enable the wheel events, that is not being performed by addAWTEventListener().

Unfortunately making the GLCanvas focussable didn’t help, too.

This is exactly the workaround, that I currently use in HIAL ;).

Marvin