Returns true if parameter is a 'double click event' - Java Swing

Java examples for Swing:Mouse Event

Description

Returns true if parameter is a 'double click event'

Demo Code

/*// w ww . ja  v a 2  s.  co  m
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
//package com.java2s;
import java.awt.event.MouseEvent;

public class Main {
    private static int DOUBLE_CLICK_DELTA = 300;
    /** variable for double click */
    private static int tempx = 0;
    private static int tempy = 0;
    private static long temph = 0;
    private static int tempm = 0;

    /** Returns true if parameter is a 'doubleclick event'
     * @param e MouseEvent
     * @return true if the event is a doubleclick
     */
    public static boolean isDoubleClick(MouseEvent e) {
        // even number of clicks is considered like doubleclick
        // it works as well as 'normal testing against 2'
        // but on solaris finaly works and on Win32 works better
        //System.out.println ("Click COunt: "+e.getClickCount ()); // NOI18N
        return (e.getClickCount() % 2 == 0) || isDoubleClickImpl(e);
    }

    /** Tests the positions.
     */
    private static boolean isDoubleClickImpl(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        long h = e.getWhen();
        int m = e.getModifiers();
        //System.out.println ("When:: "+h); // NOI18N
        // same position at short time
        if (tempx == x && tempy == y && h - temph < DOUBLE_CLICK_DELTA
                && m == tempm) {
            // OK forget all
            tempx = 0;
            tempy = 0;
            temph = 0;
            tempm = 0;
            return true;
        } else {
            // remember
            tempx = x;
            tempy = y;
            temph = h;
            tempm = m;
            return false;
        }
    }
}

Related Tutorials