检测摆动中(框架和组件)的焦点
我出现了一个小对话框框架,该框架内有一系列按钮和一个文本框。 我需要框架能够检测用户何时将焦点放在屏幕上的其他内容上(即:框架及其组件之外的任何内容),以便我可以关闭框架。 关于如何解决这个问题有什么建议吗?我已经尝试了几个小时的焦点解决方案,但没有解决方案!
I have a small dialog frame that appears, and within this frame are a series of buttons and a textbox.
I need the frame to be able to detect when the user has put focus on something else on the screen (being: anything besides the frame and its components), so I can close down the frame.
Any advice on how to go about this? I've been trying at focus solutions for hours, to no solution!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 WindowStateListener
它提供的 WindowEvent 参数可以通过 getNewState() 方法告诉您窗口是否失去焦点。
Try using a WindowStateListener
The WindowEvent parameter it provides can tell you if the window has lost focus through the getNewState() method.
使用 WindowListener 并侦听 windowDeactivated。
Use a WindowListener and listen for windowDeactivated.
监听 KeyboardFocusManager 的属性“permanentFocusOwner”的属性更改。收到通知后,检查新的 focusOwner 是否位于框架下的子层次结构中,如果不是 - 关闭框架。
编辑:看到建议 Window/StateListener 的答案 - 对于顶级窗口来说,它们比我的更好:-) 对于层次结构中更深处的容器来说,监听键盘焦点管理器是一个好方法,在 JTable 的 CellEditorRemover 中实现 fi (决定是否应终止待处理的编辑)
listen to property changes of the property "permanentFocusOwner" of the KeyboardFocusManager. On being notified, check if the new focusOwner is in the child hierarchy under the frame, if not - close the frame.
Edit: seeing the answers suggesting a Window/StateListener - they are better than mine for a top-level window :-) Listening to the keyboardFocusManager is a good approach for containers deeper down in the hierarchy, implemented f.i. in the CellEditorRemover of a JTable (to decide if a pending edit should be terminated)