如何防止通过单击 JPopupMenu 外部来关闭 JPopupMenu 时触发其他事件?

发布于 2024-11-13 22:35:51 字数 273 浏览 3 评论 0原文

我想用 JPopupMenu 复制右键单击上下文菜单的一些属性:

  1. 当菜单打开并单击其他位置时,菜单将关闭。
  2. 当菜单打开并单击其他位置时,不会发生其他情况。

我已经把第一部分写得很好了。但是当我单击其他地方时,可能会发生其他事件。例如,假设我有按钮 A,它执行某些操作 B。当前,如果 JPopupMenu 打开,并且我单击 A,则 JPopupMenu 关闭并执行 B。我希望 JPopupMenu 关闭并且 B 不被执行。这可能吗?

谢谢

There are some properties of the right-click context menu I would like to replicate with a JPopupMenu:

  1. When menu is open and you click elsewhere, menu closes.
  2. When menu is open and you click elsewhere, nothing else happens.

I've got the first part down just fine. But when I click elsewhere, other events can occur. For instance, lets say I have button, A, which performs some action, B. Currently, if the JPopupMenu is open, and I click A, the JPopupMenu closes and B is performed. I would prefer that JPopupMenu close and B NOT be performed. Is this possible?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

雨巷深深 2024-11-20 22:35:51

这有效,并且更简单......尽管可能会被某些外观和感觉覆盖。

UIManager.put("PopupMenu.consumeEventOnClose", Boolean.TRUE);

还值得注意的是,这仅消耗了 MOUSE_PRESSED 事件,后续的 MOUSE_CLICKED 事件不会被消耗。您可以通过在 mousePressed() 中设置标志并在 mouseReleased() 中测试它来模拟鼠标单击。如果初始按下的鼠标被消耗,则不会在 mouseReleased() 中设置该标志

private boolean pressed = false;

@Override
public void mousePressed(MouseEvent e) {
    pressed = true;
}

@Override
public void mouseReleased(MouseEvent e) {
    if (pressed) {
         // do click stuff
    }
    pressed = false;
}

This works, and is a lot simpler... although could be overridden by some look and feels.

UIManager.put("PopupMenu.consumeEventOnClose", Boolean.TRUE);

It is also worth noting this only consumes the MOUSE_PRESSED event, the subsequent MOUSE_CLICKED event is not consumed. You can emulate mouse clicked by setting flag in mousePressed() and testing it in mouseReleased(). If the initial mouse pressed is consumed then the flag won't be set in the mouseReleased()

private boolean pressed = false;

@Override
public void mousePressed(MouseEvent e) {
    pressed = true;
}

@Override
public void mouseReleased(MouseEvent e) {
    if (pressed) {
         // do click stuff
    }
    pressed = false;
}
稳稳的幸福 2024-11-20 22:35:51

考虑到您的问题和评论中所说的内容,我将通过以下方式之一解决您的问题。

从技术上讲,您在这里有两个选择:

1.每当用户将鼠标移到弹出窗口之外时隐藏弹出窗口。这样您就不会遇到用户点击的问题,因为弹出窗口会自行消失。

2.全局捕获这个特定的鼠标事件,如果弹出窗口可见,则在左键单击时使用该事件。我在下面的示例中展示了这个特定的解决方案。

    import java.awt.AWTEvent;
    import java.awt.Toolkit;
    import java.awt.event.AWTEventListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JPanel;
    import javax.swing.JPopupMenu;
    import javax.swing.SwingUtilities;

    public class DisableClickWhenPopupVisibleTest
    {
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {               
                    final JPopupMenu popup = new JPopupMenu();
                    popup.add(new JMenu("aAaa"));
                    JPanel contentPane = new JPanel();
                    contentPane.add(popup);
                    JButton b = new JButton();
                    b.setAction(new AbstractAction("Button")
                    {
                        private static final long serialVersionUID = 1L;
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                            System.out.println("b actionPerformed");
                        }
                    });
                    contentPane.add(b);
                    contentPane.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e)
                        {
                            showPopup(e);
                        }
                        @Override
                        public void mouseReleased(MouseEvent e)
                        {
                            showPopup(e);
                        }
                        private void showPopup(MouseEvent e)
                        {
                            if(e.isPopupTrigger())
                                popup.show(e.getComponent(), e.getX(), e.getY());
                        }
                    });
                    //use global mouse event capture to disable left click on anything when popup is visible
                    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                        @Override
                        public void eventDispatched(AWTEvent event)
                        {
                            MouseEvent me = (MouseEvent)event;
                            if(me.getID() == MouseEvent.MOUSE_PRESSED)
                            {
                                System.out.println("eventDispatched popup.vis="+popup.isVisible());
                                if( me.getButton() == MouseEvent.BUTTON3)
                                {   
                                    System.out.println("BUTTON3");
                                }   
                                else if(me.getButton() == MouseEvent.BUTTON1)
                                {
                                    System.out.println("BUTTON1");
                                    if(popup.isVisible())
                                        me.consume();
                                }
                            }
                        }
                    }, AWTEvent.MOUSE_EVENT_MASK);                      
                    JFrame f = new JFrame();
                    f.setContentPane(contentPane);
                    f.setSize(400, 300);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setVisible(true);
                }
            });
        }
    }

您可以通过右键单击按钮左侧来测试示例,然后将显示弹出窗口。然后,如果您单击该按钮,则不会调用其操作。如果弹出窗口隐藏,则通常会调用该操作。此功能由以下代码行 Toolkit.getDefaultToolkit().addAWTEventListener(...) 提供。您可以注释掉该行并观察到该操作在您当前遇到的任何情况下都会发生。

Taking into account what was said in your question and comments, I would approach your problem in one of the following ways.

Technically you have two options here:

1.Hide the popup whenever user moves the mouse outside of the popup. This way you do not have the problem of user clicking since the popup will disappear itself.

2.Capture this particular mouse event globally and consume the event on left click if the popup is visible. I show this particular solution in the example below.

    import java.awt.AWTEvent;
    import java.awt.Toolkit;
    import java.awt.event.AWTEventListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JPanel;
    import javax.swing.JPopupMenu;
    import javax.swing.SwingUtilities;

    public class DisableClickWhenPopupVisibleTest
    {
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {               
                    final JPopupMenu popup = new JPopupMenu();
                    popup.add(new JMenu("aAaa"));
                    JPanel contentPane = new JPanel();
                    contentPane.add(popup);
                    JButton b = new JButton();
                    b.setAction(new AbstractAction("Button")
                    {
                        private static final long serialVersionUID = 1L;
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                            System.out.println("b actionPerformed");
                        }
                    });
                    contentPane.add(b);
                    contentPane.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e)
                        {
                            showPopup(e);
                        }
                        @Override
                        public void mouseReleased(MouseEvent e)
                        {
                            showPopup(e);
                        }
                        private void showPopup(MouseEvent e)
                        {
                            if(e.isPopupTrigger())
                                popup.show(e.getComponent(), e.getX(), e.getY());
                        }
                    });
                    //use global mouse event capture to disable left click on anything when popup is visible
                    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                        @Override
                        public void eventDispatched(AWTEvent event)
                        {
                            MouseEvent me = (MouseEvent)event;
                            if(me.getID() == MouseEvent.MOUSE_PRESSED)
                            {
                                System.out.println("eventDispatched popup.vis="+popup.isVisible());
                                if( me.getButton() == MouseEvent.BUTTON3)
                                {   
                                    System.out.println("BUTTON3");
                                }   
                                else if(me.getButton() == MouseEvent.BUTTON1)
                                {
                                    System.out.println("BUTTON1");
                                    if(popup.isVisible())
                                        me.consume();
                                }
                            }
                        }
                    }, AWTEvent.MOUSE_EVENT_MASK);                      
                    JFrame f = new JFrame();
                    f.setContentPane(contentPane);
                    f.setSize(400, 300);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setVisible(true);
                }
            });
        }
    }

You can test the example by right clicking slightly on the left of the button then the popup will show. Then if you click over the button its action will not be called. The action is called normally if the popup is hidden. This functionality is provided by the following line of code Toolkit.getDefaultToolkit().addAWTEventListener(...). You can comment out the line and observe that then the action will occur in any case as you experience it currently.

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