Personal License


  • Perpetual license (does not expire)
  • 1 application
  • Can distribute binary products only
  • Non-commercial use only
  • Attribution required
Free Read License

Developer License


  • Perpetual license (does not expire)
  • Unlimited projects
  • Can distribute code and binary products
  • Commercial use allowed
  • 1 year support
$4.99 Read License

14 Day money-back guarantee

Full refund within 14 days of purchase date.

You need to log-in or create an account
  • Create an account
  • Log-in

Please use your real name.

Activation link will be sent to this address.

Minimum 8 characters

Enter your password again

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

  • Released: May 2, 2011
    Last Update: May 2, 2011
  • Language: Java
  • Category: User Interface
  • Time / costs savings: 20h / $1200 *
(1 ratings)

DoubleClickDetector

DoubleClickDetector
Developed by Ralph Chapin, Released May 2, 2011

Detect double click mouse events in Java, Swing or AWT, and act on them.

Java

Tags: awt , click , code , count

A specialized mouse listener that detects and acts on double-clicks. Simply add it as a MouseListener to any component and add code to do the double-click work. It will react to a double click count even when the second click is not on the exact same pixel as the first click. It plays well with other mouse listeners, ignoring "consumed" mouse events and only consuming events when it actually does something.

(The movement of the mouse between the clicks of a double-click is what makes it so hard for some people to double-click with a mouse. Consuming ignored mouse clicks leads to users who are attempting several single-clicks in a row having to click twice for each plain single-click, or else clicking vveerryy sslloowwllyy.)

DoubleClickDetector is best used in a Swing or Awt environment as opposed to Graphics2D. Graphics, if at all interactive, usually requires an elaborate MouseListener implementation with plenty of instance fields and lots of code for each event type. But even with graphics, this code can serve as a proper example of how to handle a double-click.

DoubleClickDetector is fully Javadoc'ed and the source code includes a usage example.

Back to top

Usage Example

package doubleclick;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

/** A window to demo double-clicking.  Single clicks will increment the
 * number in the JLabel.  Double clicks will cause the number to become
 * zero if it is eight or larger.  Hence, while the number is six or
 * less a double-click will do nothing and the second click will act
 * as a second single click.  A double-click when the number is seven
 * will set it to zero:  the first click bumps it up to eight and the
 * double-click code sees it as eight and zeroes it. */
public class DoubleClickExample  extends JFrame  {

    /** Creates and makes visible a window. */
    public static void main( String[] args )  {
        new DoubleClickExample().setVisible( true );
    }

    /** Mouse listener that increments a number. */
    private MouseListener  inc = new MouseAdapter()  {
        public void mousePressed( MouseEvent me )  {
            if (me.isConsumed())
                // Someone else already used this click.  Do nothing.
                return;
            number += 1;
            nLabel.setText( Integer.toString( number ) );
            me.consume();
        }
    };

    /** Double-click listener that turns a number greater than 8
     * into a zero. */
    private DoubleClickDetector.Listener
                dcdListen = new DoubleClickDetector.Listener()  {
        public boolean doubleClick( MouseEvent me )  {
            if (number < 8)
                // Double-click did nothing.  Allow next mouse listener
                // to see second click as a simple "Mouse Pressed" event.
                // (Set return to "true" to be seriously annoying.)
                return false;
            number = 0;
            nLabel.setText( "0" );
            // Double-click did something.  Prevent next mouse listener
            // from reacting to second click.
            return true;
        }
    };

    private int     number = 0;
    private JLabel  nLabel = new JLabel( "0" );

    /** Creates a new DoubleClickExampleWindow. */
    public DoubleClickExample()  {
        setBounds( 100, 100, 280, 160 );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        Container  cp = getContentPane();
        cp.setLayout( null );

        nLabel.setBounds( 80, 50, 120, 24 );
        nLabel.setBackground( Color.WHITE );
        nLabel.setOpaque( true );
        nLabel.setBorder( BasicBorders.getTextFieldBorder() );
        nLabel.setHorizontalAlignment( JLabel.CENTER );
        add( nLabel );

        // Adds a double-click listener so it can intercept,
        // if it should, other mouse listeners.
        cp.addMouseListener( new DoubleClickDetector( dcdListen ) );

        // Add mouse listener to increment number.
        cp.addMouseListener( inc );
    }
}
Back to top

Javadoc for DoubleClickDetector

Javadoc for DoubleClickDetector in plain text:

doubleclick Class DoubleClickDetector

java.lang.Object doubleclick.DoubleClickDetector

All Implemented Interfaces: java.awt.event.MouseListener, java.util.EventListener

public class DoubleClickDetector extends java.lang.Object implements java.awt.event.MouseListener

A handy MouseListener implementation that detects double-clicks. This detector will pick up the second click even if the mouse moves a bit (up to 10 pixels) between clicks, so beginning mouseketteers will not be so much fun to watch, but otherwise it works very well. The second click is consumed, to keep it from causing other problems.

To use, create a DoubleClickDetector and add a DoubleClickDetector.Listener to it. Then add it as a MouseListener to a java.awt.Component. Note that mouse listeners are invoked in the order in which they are added to a component. If a previously added listener consumes a Mouse Pressed MouseEvent before this listener gets it, this listener will ignore it as the second (but not first) click in a double-click. (Other consumed mouse events are not ignored; once a Mouse Pressed event comes through, consumed or unconsumed, DoubleClickDetector is irrevocably committed to detecting a double-click.)

Java Windows note: if a Component has a mouse listener added to it, its parent will not receive mouse click events from its location. Double-click philosophical note: a double click is supposed to do a second action after the action of the first click. It should always do the same thing as a single click. It should not do a single click twice unless the double-click is unable to do its second action. In that case a double-click should work just like two single-clicks.

Mouse events happen only on the Event Queue. Adding, removing, and getting listeners may be done on any thread at any time; the class is thread-safe.

Nested Class Summary

static interface DoubleClickDetector.Listener The listener interface for detecting double clicks.

Constructor Detail

DoubleClickDetector()

public DoubleClickDetector()

Creates a new DoubleClickDetector.

DoubleClickDetector()

public DoubleClickDetector(DoubleClickDetector.Listener l)

Creates a new DoubleClickDetector and adds a Listener to it. (Turns a two-step process into a one-step process.)

Parameters:

  • l - a listener.

Method Detail

addListener()

public void addListener(DoubleClickDetector.Listener l)

Adds a listener for double-clicks. These will be checked in the order added. (If the first listener does not use the double-click, the second will be notified. If the second does use it, no more listeners will be called and all mouse events relating to the double-click will be marked as consumed. The use of more than one listener will be rare.)

removeListener()

public void removeListener(DoubleClickDetector.Listenerl)

Removes a listener for double-clicks.

getListeners()

public DoubleClickDetector.Listener[] getListeners()

Returns an independent array of listeners, in order. Never returns null, but may return an empty array (an array of length zero).

Back to top

Javadoc for DoubleClickDetector.Listener

Javadoc for DoubleClickDetector.Listener in plain text:

doubleclick Interface DoubleClickDetector.Listener

Enclosing class: DoubleClickDetector

public static interface DoubleClickDetector.Listener

The listener interface for detecting double clicks.

Method Detail

doubleClick()

boolean doubleClick(java.awt.event.MouseEvent e)

Called when a valid double click is detected.

Parameters: * 'e' - the event for the first mouse click. Will be a Mouse Released event.

Returns: true if the double click was accepted, false if it was not used. If true, the (second) click will be consumed. If false, this click will not be consumed, and hopefully will register as an independent, valid click in its own right in some other MouseListener. (Conceivably, the discarded second click could end up as the second click in a second DoubleClickDetector object.)

View all 1 reviews »

User Reviews

  • Robo Flopter 1 year ago
    Does exactly what I was expecting. No more, no less.
    Was implemented in 5 Minutes. Thank you.
    Flag
    Was this helpful? Yes No

Questions & Comments


Or enter your name and Email
No comments have been posted yet.
You must be logged-in to vote. Log-in to your account or register now.