强制 JPopupMenu 禁用其所有者 JFrame 上的悬停效果?

发布于 2024-10-20 07:18:10 字数 197 浏览 7 评论 0原文

当我右键单击 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-10-27 07:18:10

解决此问题的一种方法是在 GUI 元素之一中设置一个实例变量来标记是否启用悬停事件。我在下面展示了它的工作原理,但它不是完整的形式,您还需要在 JPopupMenu 关闭时重新启用悬停,并在触发之前检查 ENABLE_HOVER 字段的状态悬停效果。

public MyTable extends JTable {

    private boolean ENABLE_HOVER = true;

    public MyTable() {
    ...
    this.addMouseListener(new MouseListener(){
        ...
        public void mouseClicked(MouseEvent e) {
            if (isRightClick(e)) {
                setHoverEnabled(false);
                showJPopupMenu();
            }
        }
    });
    }

    protected void setHoverEnabled(final boolean hover) {
        this.ENABLE_HOVER = 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.

public MyTable extends JTable {

    private boolean ENABLE_HOVER = true;

    public MyTable() {
    ...
    this.addMouseListener(new MouseListener(){
        ...
        public void mouseClicked(MouseEvent e) {
            if (isRightClick(e)) {
                setHoverEnabled(false);
                showJPopupMenu();
            }
        }
    });
    }

    protected void setHoverEnabled(final boolean hover) {
        this.ENABLE_HOVER = hover;
    }
}
始终不够 2024-10-27 07:18:10

另一种可能更适合禁用多个但已启用的元素的方法是拦截玻璃窗格处的事件。 此处显示了其工作原理的示例。但请注意,如果您的界面已经构建,则可能需要对组件类进行大量重新调整。

您将需要拦截玻璃窗格中的所有事件,如果启用了悬停(不显示弹出菜单),您将把事件传递给适当的组件。否则,如果悬停被禁用并且 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文