强制 JPopupMenu 禁用其所有者 JFrame 上的悬停效果?
当我右键单击 JFrame 中的 JTable 时,我会显示 JPopupMenu。如果我让 JPopupMenu 按原样显示并用鼠标移动到 JTable,我仍然可以将鼠标悬停在其行上。
这不是 Windows 应用程序的默认行为。在正常情况下,如果程序中出现弹出菜单,它会阻止弹出所有者窗口上的任何悬停操作。
我可以用 Java 做同样的事情吗?
When i right click on a JTable in a JFrame I show a JPopupMenu. If I left this JPopupMenu shown as it is and moved with the mouse to the JTable I can still hover on its rows.
This is not the default behavior of Windows applications. In normal case if a popup menu appears in a program it blocks any hover actions on the popup owner window.
Can i do the same thing in Java ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决此问题的一种方法是在 GUI 元素之一中设置一个实例变量来标记是否启用悬停事件。我在下面展示了它的工作原理,但它不是完整的形式,您还需要在 JPopupMenu 关闭时重新启用悬停,并在触发之前检查
ENABLE_HOVER
字段的状态悬停效果。One way to approach this problem is to set an instance variable in one of your GUI elements to flag whether or not to enable hover events. I have shown below how this may work, but it's not in its complete form, you will also need to re-enable hover when the JPopupMenu is dismissed, and also check the state of the
ENABLE_HOVER
field before firing hover effects.另一种可能更适合禁用多个但已启用的元素的方法是拦截玻璃窗格处的事件。 此处显示了其工作原理的示例。但请注意,如果您的界面已经构建,则可能需要对组件类进行大量重新调整。
您将需要拦截玻璃窗格中的所有事件,如果启用了悬停(不显示弹出菜单),您将把事件传递给适当的组件。否则,如果悬停被禁用并且 JPopupMenu 上发生的 MouseEvent 仅传递到 JPopupMenu。
Another method which may be better suited to disabling multitudes of however enabled elements is to intercept the events at the glass pane. An example of how this might work is shown here. Be warned though if your interface is already built it may require significant re-jigging of your component classes.
You will need to intercept all events at the glass pane, if hover is enabled (no popup menu shown) you would pass the event to the appropriate component. Otherwise if hover is disabled and the MouseEvent occurred over the JPopupMenu is passed only to the JPopupMenu.