September 03, 2010, 11:51:44 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Detect mouse doubleclick  (Read 2723 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« on: August 11, 2008, 10:25:48 AM »

A quick way to find out if a user made a doubleclick with their mouse is to implement a MouseListener in the application and add this in your MouseClicked() method

Code
GeSHi (java):
  1. public void mouseClicked(MouseEvent e) {
  2. doubleClicked = doubleClicked+1;
  3. if(doubleClicked ==2){
  4. System.out.println("double clicked");
  5. doubleClicked =  0;
  6. }
Created by GeSHI 1.0.7.20

However, this will always register the 2nd click as the doubleclick and you don't want that, i've rewritten my code so that the doubleclick only shows up when the mouse is in the same position as before and the user has clicked twice within the same position.

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. MouseDoubleClick class
  3.  *****************
  4. By: deAppel http://www.engineeringserver.com - http://www.javaforums.net
  5. Contact: info [@] engineeringserver.com
  6. Version: 11/August/2008
  7. Last updated: 11/August/2008
  8.  *****************
  9. Note: MouseDoubleClick demo
  10. //----------------------------------------------------------------------*/
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13.  
  14. import javax.swing.JFrame;
  15.  
  16. public class MouseDoubleClick extends JFrame implements MouseListener{
  17. int doubleClicked = 0;
  18. int positionX = 0;
  19. int positionY = 0;
  20. public MouseDoubleClick (){
  21.  
  22. addMouseListener(this);
  23. setTitle("Mouse doubleclick demo");
  24. setLocationRelativeTo(null);
  25. setSize(200,200);
  26. setVisible(true);
  27. }
  28.  
  29. public static void main(String[] args){
  30. new MouseDoubleClick();
  31. }
  32.  
  33. public void mouseClicked(MouseEvent e) {
  34. doubleClicked = doubleClicked+1;
  35. if(doubleClicked ==2 && positionX == e.getX() && positionY == e.getY()){
  36. System.out.println("double clicked");
  37. doubleClicked =  0;
  38. }
  39. if(doubleClicked > 2){
  40. doubleClicked =  0;
  41. }
  42. positionX  = e.getX();
  43. positionY  = e.getY();
  44. }
  45.  
  46. public void mouseEntered(MouseEvent e) {}
  47. public void mouseExited(MouseEvent e) {}
  48. public void mousePressed(MouseEvent e) {}
  49. public void mouseReleased(MouseEvent e) {}
  50. }
  51.  
Created by GeSHI 1.0.7.20
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
« on: August 11, 2008, 10:25:48 AM »

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.232 seconds with 32 queries.

Google visited last this page Today at 01:30:10 PM