简体   繁体   English

Swing Mouse单击JMenu和JPopupMenu的事件

[英]Swing Mouse Click Event for JMenu and JPopupMenu

I would like to have Eclipse behavior for mouse clicks outside menus and popup menus in Swing. 我希望在Swing的菜单和弹出菜单外点击鼠标的Eclipse行为。 Basically, in Eclipse, when you press the mouse OUTSIDE the menu, the menu disappears and the mouse press event is forwarded to the component on which you clicked with the mouse. 基本上,在Eclipse中,当您在菜单外按鼠标时,菜单消失,鼠标按下事件将转发到您用鼠标单击的组件。

Irritatingly, Swing does not do this, and I can't find a way around it. 令人恼火的是,Swing没有这样做,我无法找到解决方法。

Right now, even with Windows LAF, i have to click a second time to get the component to register the mouse click. 现在,即使使用Windows LAF,我也必须再次单击以使组件注册鼠标单击。

For the sake of the response just do it on a table 为了响应,只需在桌子上进行

final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem viewProfile = new JMenuItem("View Profile");
viewProfile.addActionListener(new ActionListener() { 
   public void actionPerformed(ActionEvent e) {
   }
});
popupMenu.add(viewProfile);
table.setComponentPopupMenu(popupMenu);

EDIT: here is a test code. 编辑:这是一个测试代码。 I don't think it is convenient to add a mouse listener to every simple component so they can register mouse clicks after a popup menu. 我不认为将鼠标监听器添加到每个简单组件是很方便的,这样他们就可以在弹出菜单后注册鼠标点击。 In the following example, the Table does implement it, but not the Button. 在下面的示例中,Table确实实现了它,但没有实现Button。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestMouse {

   public static void main(String[] args) {
      new TestMouse();
   }

   private JLabel  counter;

   private int     count;

   private JPanel  northPanel;

   private JButton clickMe;

   public TestMouse() {
      EventQueue.invokeLater(new Runnable() {

         @Override
         public void run() {
            try {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            DefaultTableModel model = new DefaultTableModel(new Object[] { "A", "B", "B", "B", "B", "B", "B" }, 10);
            JTable table = new JTable(model);

            northPanel = new JPanel();

            clickMe = new JButton("Button");
            clickMe.addActionListener(new ActionListener() {

               @Override
               public void actionPerformed(ActionEvent e) {
                  System.out.println("clicked");
                  count++;
               }
            });
            counter = new JLabel("0");

            northPanel.add(counter);
            northPanel.add(clickMe);

            final JPopupMenu popupMenu = new JPopupMenu();
            JMenuItem viewProfile = new JMenuItem("View Profile");
            viewProfile.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
               }
            });
            popupMenu.add(viewProfile);
            table.setComponentPopupMenu(popupMenu);
            table.addMouseListener(new MouseAdapter() {

               @Override
               public void mouseClicked(MouseEvent e) {
                  System.out.println("clicked");
                  count++;
                  counter.setText(String.valueOf(count));
               }

            });

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(northPanel, BorderLayout.NORTH);
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
         }
      });
   }

}

I don't seem to be able to replicate the issue. 我似乎无法复制这个问题。

When I right click the table, the popup menu appears and when I click the table again (to dismiss the popup), the mouseClicked event is triggered. 当我右键单击该表时,会出现弹出菜单,当我再次单击该表时(要关闭弹出窗口),将触发mouseClicked事件。

ClicktyClack

Note the counter in the north position... 注意北方位置的计数器......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestMouse {

    public static void main(String[] args) {
        new TestMouse();
    }

    private JLabel counter;
    private int count;

    public TestMouse() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                DefaultTableModel model = new DefaultTableModel(
                        new Object[]{"A", "B", "B", "B", "B", "B", "B"},
                        10
                );
                JTable table = new JTable(model);
                counter = new JLabel("0");

                final JPopupMenu popupMenu = new JPopupMenu();
                JMenuItem viewProfile = new JMenuItem("View Profile");
                viewProfile.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                    }
                });
                popupMenu.add(viewProfile);
                table.setComponentPopupMenu(popupMenu);
                table.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.out.println("clicked");
                        count++;
                        counter.setText(String.valueOf(count));
                    }

                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(counter, BorderLayout.NORTH);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

A actual runnable example that demonstrates your problem would involve less guess work and better responses 演示您的问题的实际可运行示例将涉及较少的猜测工作和更好的响应

关闭JMenuBar弹出窗口时我遇到了完全相同的问题,这对我有用:

UIManager.put("PopupMenu.consumeEventOnClose", false);

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

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