在存在另一个可聚焦组件的情况下,AWT Canvas 无法获得焦点
我有一个带有 JTextField 和 AWT Canvas 的简单 GUI(为了防止有人反问我为什么使用 AWT Canvas:我需要一个窗口句柄)。
Canvas 是用来处理输入事件的,这意味着它必须是可聚焦的。我通过在其构造函数中使用 setFocusable(true)
来确保这一点,稍后使用 isFocusable()
检查确认它确实是可聚焦的。
现在,当 GUI 打开时,JTextField 默认获得焦点。到目前为止我觉得还好。但是,无法将焦点从该 JTextField 移开。
文章 “AWT Focus 子系统” ” 明确指出,如果单击可聚焦组件,它将获得焦点。这种情况不会发生,事实上,只有当窗口被停用并再次激活时,我才收到零焦点更改事件,但随后焦点又回到了 JTextField。
显式调用 requestFocus()
和 requestFocusInWindow()
也无济于事,后者始终返回 false
。
如果我替换 JTextField,我会得到与任何可聚焦组件相同的结果。如果 Canvas 是唯一可聚焦的容器,则一切正常,因为它始终具有焦点。
我在这里错过了什么吗?有什么方法可以让我的画布在存在另一个可聚焦组件的情况下获得焦点,最好不要使该组件无法聚焦?
I have a simple GUI with a JTextField and an AWT Canvas (to prevent the counter-question as to why I'm using an AWT Canvas: I need to have a window handle).
The Canvas is to process input events, that means it must be focusable. I assure this by using setFocusable(true)
in its constructor, later checks using isFocusable()
confirm that it is indeed focusable.
Now, the JTextField gains the focus by default when the GUI opens. That's fine by me so far. However, there is no way to get the focus away from that JTextField.
The article "The AWT Focus Subsystem" clearly states that if a focusable component is being clicked on, it will gain the focus. This does not happen, in fact, I receive zero focus change events whatsoever, only if the window gets deactivated and activated again, but then the focus is right back to the JTextField.
Explicit invocations of requestFocus()
and requestFocusInWindow()
do not help either, the latter always returns false
.
I have gotten the same results with any focusable component if I replace the JTextField. If the Canvas is the only focusable container, everything works fine because it will always have the focus.
Am I missing something here? Is there any way I can make my Canvas gain focus in the presence of another focusable component, preferably without making that one unfocusable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上在挥杆焦点方面获得了第一。 在大多数完成的 GUI 中,左(到右)JComponent 位于顶部
,如果(与创建 JComponent 一起)向 JComponent 添加监听器,那么这些监听器(fe 文档)可以获得焦点......
但在启动时对我有用:
basically in swing focus gained 1st. left(ToRight) JComponents on the top
in most completed GUI, and if there (together with creating JComponents) are added Listeners to the JComponents, then these Listeners (f.e. Document) can take focus...
but works for me on startUp:
很抱歉遗漏了一些事实证明是问题根源的信息。
如前所述,我使用的是重量级组件,因此我有一个窗口句柄。我需要一个,因为它被传递到本机库中的 OpenGL 应用程序,然后将 AWT 画布用作渲染画布。
在 Windows 中,Java 使用 GWLP_USERDATA 窗口字段来存储指向 AWTComponent 对象的指针。然而,所述 OpenGL 应用程序覆盖该字段以存储其自己的 Window 对象指针,这当然会破坏所有 AWT 相关功能。
我通过创建一个自定义窗口消息处理程序解决了这个问题,该处理程序将传入消息委托给 OpenGL 应用程序和 Java 的 AWT 部分。
Sorry for leaving some info out that turned out to be the root of the problem.
As mentioned, I'm using a heavyweight component so I have a window handle. I need one because it is passed to an OpenGL application in a native library, the AWT canvas is then used as a rendering canvas.
In Windows, Java uses the GWLP_USERDATA window field to store a pointer to an AWTComponent object. However, said OpenGL application overrides that field to store its own Window object pointer, which will of course break all AWT related functionality.
I solved this problem by creating a custom window message handler that delegates incoming messages to both the OpenGL application and Java's AWT part.