With the timer delay to achieve the mouse double click event events, click and double-click events independently of each other!

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Timer;

/**
 *  Use the timer delay click event implements mouse double-click event  , Click and double-click event from interfering with each other  !
 */
public class UserMouseAdapter extends MouseAdapter {

        private boolean flag = false;//  Use to determine whether has been executed the double-click event  
        private int clickNum = 0;//  Use to determine whether the execution double-click event  

        public void mouseClicked(MouseEvent e) {
                final MouseEvent me = e;//  The event source  

                this.flag = false;//  Each click of the mouse initialization double-click event flag to false  

                if (this.clickNum == 1) {//  When clickNum  ==1 When you perform a double-click event  
                        this.mouseDoubleClicked(me);//  Perform a double-click event  
                        this.clickNum = 0;//  Initializes a double-click event flag to 0  
                        this.flag = true;//  Double-click the event have already been performed  , Event flag to true  
                        return;
                }

                //  Define the timer  
                Timer timer = new Timer();

                //  Timer start  , Delay-0  .2 Seconds after the click event  
                timer.schedule(new java.util.TimerTask() {
                        private int n = 0;//  Record timer executions  

                        public void run() {
                                if (flag) {//  If you double-click event has executed  , Then click the perform directly cancel  
                                        n = 0;
                                        clickNum = 0;
                                        this.cancel();
                                        return;
                                }
                                if (n == 1) {//  Timer wait 0  .2 Seconds  , Double-click the event has not yet happened  , Perform the click event  
                                        mouseSingleClicked(me);//  Perform the click event  
                                        flag = true;
                                        clickNum = 0;
                                        n = 0;
                                        this.cancel();
                                        return;
                                }
                                clickNum++;
                                n++;
                        }
                }, new Date(), 200); //  To set the delay time  
        }

        /** */
        /**
         *  The mouse click event  
         * 
         * @param e
         *             The event source parameters  
         */
        public void mouseSingleClicked(MouseEvent e) {
                // System.out.println("Single Clicked!");
        }

        /** */
        /**
         *  Mouse double-click event  
         * 
         * @param e
         *             The event source parameters  
         */
        public void mouseDoubleClicked(MouseEvent e) {
                // System.out.println("Doublc Clicked!");
        }
}
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of With the timer delay to achieve the mouse double click event events, click and double-click events independently of each other!

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Great collection of java interview topics

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract does n

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries