2

I need to use mouseReleased method instead of mouseClicked so i need to find a way to Intercept double click. this is the code:

public void mouseReleased(MouseEvent e)
{
    if (e.getClickCount() == 2)
        System.out.println ("Double CLICK mouseReleased");
    else
    {    
        row= clientTable.rowAtPoint(e.getPoint());
        col= clientTable.columnAtPoint(e.getPoint());

        clientTable.sel(row, col);
    }
}

The problem is that when I double-click I have also a single click event. Can anyone know ho to fix that? Thanks

2
  • 1
    I need to use mouseReleased method instead of mouseClicked > Why? Commented Aug 6, 2012 at 12:51
  • Because i need to detect row selecion in a table and, If I use mouseClicked, I' ve problem with dragging: if I click and drag and during drag I change row I intercept click on the first cell clicked during dragging. Do you understand the problem? Thanks! Commented Aug 6, 2012 at 13:21

3 Answers 3

1

1. Use the method getClickCount() method of ClassMouseEvent

2. This will help you to get the Type of the click.. Single , Double, Triple, etc..

See here for more details:

http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseEvent.html

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

1 Comment

I use the method getClickCount() in my code and it works but when the double click in detected a single click is also detected and this is a problem for me. Thanks!
0
    public void mouseClicked(MouseEvent evt) { 
    if (evt.getButton()==MouseEvent.BUTTON1){
        leftClick = true; clickCount = 0;
        if(evt.getClickCount() == 2) doubleClick=true;
        Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

                   timer = new Timer(timerinterval, new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {  
                        if(doubleClick){
                            System.out.println("double click.");
                            sb = new StringBuffer();
                            sb.append("Double Click");
                            clickCount++;
                            if(clickCount == 2){
                                rfbProto.capture();
                                clickCount=0;
                                doubleClick = false;
                            }

                        } else {

                            sb = new StringBuffer();
                            sb.append("Left Mouse");
                            System.out.println("single click.");
                            rfbProto.capture();
                        }
                    }               
                });
                timer.setRepeats(false);
                timer.start();
                if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
    }           
    }

Comments

0

there is no simpel fix to not have a single click when you double click

i wrote this once but it has a downside, your single click gets abit sluggish

example usage:

    JLabel label = new JLabel("test");
    label.addMouseListener(new SingleClickInhibitorMouseListener(some other mouselistener));


public class SingleClickInhibitorMouseListener implements MouseListener
{
  public static final Integer CLICK_TRESHOLD = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

  private Collection<MouseListener> mouseListeners = new ArrayList<MouseListener>();

  public SingleClickInhibitorMouseListener( MouseListener listener )
  {
    mouseListeners.add(listener);
  }

  public void addMouseListener( MouseListener listener )
  {
    mouseListeners.add(listener);
  }

  public void removeMouseListener( MouseListener listener )
  {
    mouseListeners.remove(listener);
  }

  public void removeAllListeners()
  {
    mouseListeners.clear();
  }

  public void mouseSingleClick( MouseEvent e )
  {
    for (MouseListener mouseListener : mouseListeners)
      {
      mouseListener.mouseClicked(e);
      }
  }

  public void mouseDoubleClick( MouseEvent e )
  {
    for (MouseListener mouseListener : mouseListeners)
      {
      mouseListener.mouseClicked(e);
      }
  }

  public boolean hasDoubleClick = false;

  public void mouseClicked( final MouseEvent me )
  {
    if ( me.getClickCount() == 1 )
      {
      new Thread(new Runnable()
      {
        public void run()
        {
          try
            {
            Thread.sleep(CLICK_TRESHOLD);
            SwingUtilities.invokeLater(new Runnable()
            {
              public void run()
              {
                if ( hasDoubleClick )
                  hasDoubleClick = false;
                else
                  mouseSingleClick(me);
              }
            });
            }
          catch (InterruptedException e)
            {
            mouseSingleClick(me);
            hasDoubleClick = false;
            }
        }
      }).start();
      }
    else
      {
      if ( me.getClickCount() == 2 )
        hasDoubleClick = true;
      mouseDoubleClick(me);
      }
  }

  public void mousePressed( MouseEvent e )
  {
    for (MouseListener mouseListener : mouseListeners)
      {
      mouseListener.mousePressed(e);
      }
  }

  public void mouseReleased( MouseEvent e )
  {
    for (MouseListener mouseListener : mouseListeners)
      {
      mouseListener.mouseReleased(e);
      }
  }

  public void mouseEntered( MouseEvent e )
  {
    for (MouseListener mouseListener : mouseListeners)
      {
      mouseListener.mouseEntered(e);
      }
  }

  public void mouseExited( MouseEvent e )
  {
    for (MouseListener mouseListener : mouseListeners)
      {
      mouseListener.mouseExited(e);
      }
  }
}

2 Comments

Thanks for posting your code but I absolutely need something that give me good doubclick detection but also a perfect single click. Thanks!
that does not exist, a computer cannot know after the first click that the human at the mouse intents to double click so you have to simulate behaviour.

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.