Reputation: 33
In my application I'd like to forward mouse and keyboard events to an invisible JPanel and I'd like that JPanel to process them as if they came from the EDT. The reason for this is because the application has an optional extension to use JOGL for some rendering. I have already written the GUI stuff in Swing and so I'd like to reuse that code. Unfortunately, you can't mix Swing with JOGL's GLCanvas (I can use GLJpanel but this is far too slow to be usable). JOGL does have a feature (Overlay), though, that allows you to draw to a Graphics instance to have it drawn on the GL context. So I can call jpanel.paint(g) to paint it to the screen using the overlay which works fine. The only piece of the puzzle left is to somehow use the mouse and keyboard listeners on the GLCanvas to receive and forward events to the JPanel.
I've tried using findComponentAt(mousex, mousey) to get a component on the JPanel at a particular location and then calling dispatchEvent to that component, but the components don't react to mouse clicks or anything. I'm assuming that the EDT does some kind of conversion from MouseEvent/KeyEvent to ActionEvent to get the buttons and textfields to work properly. If worst comes to worst, I can test if the component is a JButton and use doClick which will at least give me some functionality. But this won't work for non-button components and it won't do any button hovering, etc.,. I'm aware that I should probably just use an OpenGL GUI toolkit for this (e.g., FengGUI), but I'm relying on Swing's HTML rendering capabilities and I'd rather not try to rewrite that code.
How can I properly receive MouseEvents from the GLCanvas' and forward them to the JPanel so that the proper handling takes place? (i.e., the action listener for the buttons are activated, the buttons show a different graphic when hovered over, etc.,)
Any tips would be greatly appreciated
Upvotes: 3
Views: 255
Reputation: 205885
In How to Use Root Panes: The Glass Pane, GlassPaneDemo
shows one approach in the method redispatchMouseEvent()
.
Upvotes: 2