模拟事件分发线程
在我的应用程序中,我想将鼠标和键盘事件转发到不可见的 JPanel,并且我希望 JPanel 处理它们,就好像它们来自 EDT 一样。原因是应用程序有一个可选的扩展来使用 JOGL 进行某些渲染。我已经在 Swing 中编写了 GUI 内容,因此我想重用该代码。不幸的是,您不能将 Swing 与 JOGL 的 GLCanvas 混合使用(我可以使用 GLJpanel,但这太慢了,无法使用)。不过,JOGL 确实有一个功能(覆盖),它允许您绘制 Graphics 实例以将其绘制在 GL 上下文上。所以我可以调用 jpanel.paint(g) 使用覆盖层将其绘制到屏幕上,效果很好。剩下的唯一难题是如何使用 GLCanvas 上的鼠标和键盘侦听器来接收事件并将其转发到 JPanel。
我尝试使用 findComponentAt(mousex, mousey) 获取 JPanel 上特定位置的组件,然后调用该组件的dispatchEvent,但这些组件不会对鼠标单击或任何操作做出反应。我假设 EDT 执行某种从 MouseEvent/KeyEvent 到 ActionEvent 的转换,以使按钮和文本字段正常工作。如果最坏的情况发生,我可以测试该组件是否是 JButton 并使用 doClick 这至少会给我一些功能。但这不适用于非按钮组件,也不会执行任何按钮悬停等操作。我知道我可能应该为此使用 OpenGL GUI 工具包(例如,FengGUI),但我依赖于 Swing 的 HTML 渲染功能,并且我不想尝试重写该代码。
如何正确接收来自 GLCanvas 的 MouseEvents 并将它们转发到 JPanel 以便进行正确的处理? (即,按钮的动作侦听器被激活,按钮悬停时显示不同的图形等)
任何提示将不胜感激
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在如何使用根窗格:玻璃窗格 ,
GlassPaneDemo
显示了redispatchMouseEvent()
方法中的一种方法。In How to Use Root Panes: The Glass Pane,
GlassPaneDemo
shows one approach in the methodredispatchMouseEvent()
.