简体   繁体   English

模拟事件调度线程

[英]Simulating the Event Dispatch Thread

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.在我的应用程序中,我想将鼠标和键盘事件转发到一个不可见的 JPanel,并且我希望 JPanel 处理它们,就好像它们来自 EDT。 The reason for this is because the application has an optional extension to use JOGL for some rendering.这样做的原因是因为应用程序有一个可选的扩展来使用 JOGL 进行某些渲染。 I have already written the GUI stuff in Swing and so I'd like to reuse that code.我已经在 Swing 中编写了 GUI 内容,所以我想重用该代码。 Unfortunately, you can't mix Swing with JOGL's GLCanvas (I can use GLJpanel but this is far too slow to be usable).不幸的是,您不能将 Swing 与 JOGL 的 GLCanvas 混合使用(我可以使用 GLJpanel,但这太慢而无法使用)。 JOGL does have a feature (Overlay), though, that allows you to draw to a Graphics instance to have it drawn on the GL context.不过,JOGL 确实有一个功能(覆盖),它允许您绘制到 Graphics 实例以使其在 GL 上下文中绘制。 So I can call jpanel.paint(g) to paint it to the screen using the overlay which works fine.所以我可以调用 jpanel.paint(g) 使用可以正常工作的叠加层将其绘制到屏幕上。 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.剩下的唯一难题就是以某种方式使用 GLCanvas 上的鼠标和键盘侦听器来接收事件并将其转发给 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.我尝试使用 findComponentAt(mousex, mousey) 在 JPanel 上的特定位置获取组件,然后向该组件调用 dispatchEvent,但组件不会对鼠标单击或任何内容做出反应。 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.我假设 EDT 进行了从 MouseEvent/KeyEvent 到 ActionEvent 的某种转换,以使按钮和文本字段正常工作。 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.如果最坏的情况发生,我可以测试该组件是否是 JButton 并使用 doClick 至少会给我一些功能。 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 (eg, FengGUI), but I'm relying on Swing's HTML rendering capabilities and I'd rather not try to rewrite that code.我知道我可能应该为此使用 OpenGL GUI 工具包(例如 FengGUI),但我依赖于 Swing 的 HTML 渲染功能,我不想尝试重写该代码。

How can I properly receive MouseEvents from the GLCanvas' and forward them to the JPanel so that the proper handling takes place?如何正确接收来自 GLCanvas' 的 MouseEvents 并将它们转发到 JPanel 以便进行正确处理? (ie, the action listener for the buttons are activated, the buttons show a different graphic when hovered over, etc.,) (即,按钮的动作监听器被激活,按钮在悬停时显示不同的图形等,)

Any tips would be greatly appreciated任何提示将非常感谢

In How to Use Root Panes: The Glass Pane , GlassPaneDemo shows one approach in the method redispatchMouseEvent() .How to Use Root Panes: The Glass Pane中, GlassPaneDemo展示了redispatchMouseEvent()方法中的一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM