在Java Swing中生成双击鼠标事件

时间:2012-03-03 19:24:40

标签: java swing mouseevent double-click

我正在尝试在EDT上生成双击鼠标事件,如下所示:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        component.dispatchEvent(new MouseEvent(
            component,                                      
            MouseEvent.MOUSE_CLICKED,
            System.currentTimeMillis(),
            InputEvent.BUTTON1_MASK,
            x, y,
            2, // click count
            false
        ));
    }
});

即使我将点击次数设置为2,这似乎也不会发送双击事件。

有任何建议或示例吗?

2 个答案:

答案 0 :(得分:3)

考虑到:

final JButton clickTwiceButton = new JButton();
final JButton fireEventButton = new JButton();

听众:

clickTwiceButton.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            JOptionPane.showMessageDialog(null, "Double clicked!");
        }
    }
});

fireEventButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        // Invoking later for no reason, just to simulate your code
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                clickTwiceButton.dispatchEvent(new MouseEvent(
                     fireEventButton,
                     MouseEvent.MOUSE_CLICKED, 
                     1,
                     MouseEvent.BUTTON1, 
                     0, 0, 
                     2, 
                     false
                ));
            }
        });
    }         
});

当我点击fireEventButton时,MouseEvent被正确发送到clickTwiceButton,对话框按预期显示。

所以,正如@Andrew所指出的那样,问题似乎是要么将事件触发到错误的组件,要么使用已注册的MouseListener / MouseAdapter代码来解决问题。< / p>

使用component.getMouseListeners()检查组件侦听器并调试处理其事件的代码。

答案 1 :(得分:0)

   public class TestMouseListener implements MouseListener {    
      private boolean leftClick;
      private int clickCount;
      private boolean doubleClick;
      private boolean tripleClick;
      public void mouseClicked(MouseEvent evt) {
        if (evt.getButton()==MouseEvent.BUTTON1){
            leftClick = true; clickCount = 0;
            if(evt.getClickCount() == 2) doubleClick=true;
            if(evt.getClickCount() == 3){
                doubleClick = false;
                tripleClick = true;
            }
            Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

                     Timer  timer = new Timer(timerinterval, new ActionListener() {
                        public void actionPerformed(ActionEvent evt) { 

                            if(doubleClick){
                                System.out.println("double click.");                                    
                                clickCount++;
                                if(clickCount == 2){
                                    doubleClick();   //your doubleClick method
                                    clickCount=0;
                                    doubleClick = false;
                                    leftClick = false;
                                }

                            }else if (tripleClick) { 

                                System.out.println("Triple Click.");
                                clickCount++;
                                if(clickCount == 3) {
                                   tripleClick();  //your tripleClick method
                                    clickCount=0;
                                    tripleClick = false;
                                    leftClick = false;
                                }

                            } else if(leftClick) {                                      
                                System.out.println("single click.");
                                leftClick = false;
                            }
                        }               
                    });
                    timer.setRepeats(false);
                    timer.start();
                    if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
        }           
  }


      public static void main(String[] argv) throws Exception {

        JTextField component = new JTextField();
        component.addMouseListener(new TestMouseListener());
        JFrame f = new JFrame();

        f.add(component);
        f.setSize(300, 300);
        f.setVisible(true);

        component.addMouseListener(new TestMouseListener());
      }

}