1

I am trying to generate a double click mouse event on the EDT as follows:

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
        ));
    }
});

This does not seem to dispatch a double click event, even though I am setting the click count to 2.

Any suggestions or examples?

3
  • 2
    What is component? In fact, don't bother to answer that. For better help sooner, post an SSCCE (which will answer that, as well as 4 out of 5 of the other questions I might ask). Commented Mar 3, 2012 at 19:33
  • 1
    Faking events is usually a bad idea. Why don't you move the code that handles the doubleclick into a separate method and call that method directly? Commented Mar 3, 2012 at 21:29
  • Why are you doing this? Is it for testing? Commented Mar 3, 2012 at 22:55

3 Answers 3

3

Considering:

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

Listeners:

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
                ));
            }
        });
    }         
});

When I click fireEventButton the MouseEvent gets correctly dispatched to clickTwiceButton, and the dialog appears as expected.

So, as @Andrew pointed out, the problem seems to be that either you are firing the event to the wrong component or that something is not right with the registered MouseListener / MouseAdapter code.

Use component.getMouseListeners() to check for your component Listeners and debug the code that handles its events.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for using the EventQueue; @user1246353 should also consider using Robot.
Hi @trashgod, it is nice to see you again. 1+ for the Robot suggestion. Just one thing (I'm too lazy to open a new question, but I think this is correlated to the OP question), since the event is being dispatched directly from a Listener, wouldn't it run on the EventQueue anyway? I'm never sure about when I really need invokeLater and invokeAndWait.
Good question: not necessarily; we aren't shown how the OP's Runnable is evoked. As long as actionPerformed() runs on the EDT, invokeLater() ensures that the MouseEvent will be dispatched after the ActionListener completes. Looking closer, I see that I overlooked the OP's similar usage, so +1 for @Override!
@Anthony Accioly nice answer +1
1

The method is very simple. You should get the time of the first click and the time of the second click, then you can do a condition in between. Method code as below:

private boolean state=false;
private long first_pressed;
JButton btnAdd = new JButton("add");
btnAdd.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            
            if(state==false) {
                first_pressed=e.getWhen();
                state=true;
            }
            if(first_pressed!=e.getWhen())
            {
                JOptionPane.showConfirmDialog(null,"doubel click","Click",JOptionPane.YES_NO_OPTION);
                state=false;
            }
        }
 });

Comments

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());
      }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.