Search code examples
swinghttp-redirectmousejava-3ddispatchevent

Redirecting Swing mouse events


I am trying to make it possible to display and interact with Java Swing components on top of a Java3D canvas. I am displaying the components by painting a transparent JPanel to a buffered image, and then painting that buffer over the canvas using J3DGraphics2D.

What I can't figure out is how to forward mouse events to the swing components in the JPanel. I want all keyboard and mouse events on the Canvas3D to be dispatched to the JPanel, and then fall back through to the Canvas3D if they aren't captured by any swing components (e.g. the mouse isn't over any of them).

I tried calling Container.dispatchEvent(AWTEvent), but it doesn't successfully dispatch the events to the proper components, even when for example the mouse cursor is right over a button in the Container.

Does anyone know a way to do this? It should be possible.


Solution

  • At long last, I figured it out! It's already been done -- use JCanvas3D and a JLayeredPane. This is the opposite approach to rendering the Swing components in postRender()-- JCanvas3D renders into an offscreen buffer, and then paints to the screen with AWT, creating a lightweight canvas that interacts properly with components in the JLayeredPane, even if they are transparent.

    One thing to watch out for -- JCanvas3D redirects all input to the offscreen Canvas3D, but at first my Orbiter didn't work like it had with a heavyweight Canvas3D. All you have to do is add mouse & key listeners to the JCanvas3D, because AWT won't even deliver those events if there are no listeners registered for them.