简体   繁体   English

在主框架之外时,JMenuItem不调用动作侦听器

[英]JMenuItem does not call action listeners when outside main frame

I have a JMenuItem in a JMenu sub-menu on the right of the main frame. 我在主框架右侧的JMenu子菜单中有一个JMenuItem。

When the sub-menu pops up it is displayed to the right of the main menu and is outside of the main frame. 弹出子菜单时,它将显示在主菜单的右侧,并且位于主框架的外部。

When the menu item is selected with a mouse click in this state, an ActionEvent is not propogated to the action listener. 在这种状态下通过单击鼠标选择菜单项时,不会将ActionEvent传播到动作侦听器。

Interestingly keyboard selection with enter does result in the action listener being called. 有趣的是,使用Enter选择键盘确实会导致调用动作侦听器。

When the main frame is expanded to fill the screen the sub-menu pops out to the left of the main menu and is thus inside the main frame. 展开主框架以填满屏幕时,子菜单会弹出到主菜单的左侧,因此位于主框架内。

In this situation the ActionEvent is propogated when the mouse is clicked in the menu item. 在这种情况下,在菜单项中单击鼠标时会传播ActionEvent。

I added a mouse listener to the JMenuItem to debug it and I noticed that MOUSE_ENTERED and MOUSE_PRESSED events do occur but not MOUSE_RELEASED events which only occur when the JMenuItem is inside the main frame. 我在JMenuItem中添加了一个鼠标侦听器以对其进行调试,并且我注意到确实发生了MOUSE_ENTERED和MOUSE_PRESSED事件,但没有发生MOUSE_RELEASED事件,这些事件仅在JMenuItem位于主机内部时发生。

I am using Java 1.7.0_45 on Windows 7 (64 bit). 我在Windows 7(64位)上使用Java 1.7.0_45。

Has anybody come else encountered this issue? 有人来过这个问题吗?

Problem was caused by the following initialization code in our application which is a workaround for an issue in Java 6: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6770457 问题是由我们的应用程序中的以下初始化代码引起的,这是Java 6中问题的解决方法: http : //bugs.sun.com/bugdatabase/view_bug.do?bug_id=6770457

    // Workaround for issue
    // (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6770457)
    PopupFactory.setSharedInstance(new PopupFactory() {

        @Override
        public Popup getPopup(Component owner, Component contents, int x, int y) {
            Window win = SwingUtilities.getWindowAncestor(owner);
            if (win.isActive()) {
                return super.getPopup(owner, contents, x, y);
            }
            return super.getPopup(JOptionPane.getRootFrame(), contents, x, y);
        }
    });
    // end fix

The solution was to modify the if statement as follows: 解决方案是修改if语句,如下所示:

            if (win.isActive() || win.getClass().getName().equalsIgnoreCase("javax.swing.Popup$HeavyWeightWindow")) {

Hopefully this will be useful to somebody else. 希望这对其他人有用。

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

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