I’ve been using Swing as my widget toolkit on OpenGL with some success but have run into a few deep problems with the way Swing relies on AWT to dispatch events and was hoping someone here might be able to help.
I’m using Agile2D to provide me with a Graphics2D object that Swing components can use to directly draw into the OpenGL frame. This is working pretty well perfectly on the visual side.
My GLCanvas lives in a panel that also contains a custom JRootPane that holds the 2d components sitting on the 3d scene (the hud). The GLCanvas receives all mouse and keyboard events and forwards them to the JRootPane which dispatches them to the components beneath (with some code v similar to that used by JInternalFrame - find the deepest component under the mouse that has a listener and retarget the event to that component). The JRootPane also kicks off the painting of the components with the Agile2D Graphics object. There are a few other wrinkles such as a custom RepaintManager that blocks repaints to such components (because they are drawn every frame anyway).
However the problems come with two special cases:
(1) The drop down menu with a JComboBox only receives keyboard events. It seems that a mouse click on an item in the menu (actually a JList with the basic UI) gets intercepted by the focus manager and (presumably) since the target is the GLCanvas as far as the focus manager is concerned, it sends a focus lost event that hides the JList before the mouse click event reaches it. I’m not very familiar with how the focus manager works - can anyone suggest a sensible way to correct this behaviour?
(2) Modal dialogs - particularly those created by JOptionPane - do something very hairy with the event queue involving a call to startLWModal (backdoor via reflection - yuck!) with the result that key events reach them but mouse events do not - perhaps because it thinks the GLCanvas and the JRootPane have different HW containers? I am really at a loss how to resolve this one.
I guess I can find alternative workaround to these particular problems - the focus manager is not so relevant in a game UI, so a custom ComboBoxUI might be one solution. Similarly I can avoid modal dialogs. But it would be nice to have all of Swing available not some irregular subset.
Incidentally when it comes to the next major revision of Swing, might I suggest cutting it completely loose from the AWT Component hierarchy and building it as an independent layer with clear event input and drawing entry points. It would certainly make it more adapatable to uses such as game UIs.