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

Luca
  • 1,704
  • 3
  • 29
  • 42
  • 1
    *I need to use mouseReleased method instead of mouseClicked* > Why? – Duncan Jones Aug 06 '12 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! – Luca Aug 06 '12 at 13:21

3 Answers3

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

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 2
    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! – Luca Aug 06 '12 at 13:01
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);
      }
  }
}
Peter
  • 5,728
  • 20
  • 23
  • Thanks for posting your code but I absolutely need something that give me good doubclick detection but also a perfect single click. Thanks! – Luca Aug 06 '12 at 14:39
  • 1
    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. – Peter Aug 07 '12 at 09:17
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();
    }           
    }
Anonymous
  • 152
  • 1
  • 1
  • 11