Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Mar 16, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Detecting Double-Clicks

When creating a Java GUI, it is often the case that you want some action to be taken when the user double-clicks on some component. Although there is no simple way to detect double-clicks in Java 1.0.2, the new 1.1 event model provides an easy way to do this. Simply call the getClickCount() method on the instance of the MouseEvent class that is passed to the mouseClicked() or mousePressed() methods which are part of the MouseListener interface. When a value of 2 is returned from getClickCount(), this indicates that the user has double-clicked on the component. For example:
import java.awt.*;
import java.awt.event.*;

public class DoubleClickTest extends Panel implements MouseListener {

	public void mousePressed(MouseEvent event) {
		if (event.getClickCount() == 2) {
			//  Executed only when the user double-clicks on
this component
			System.out.println("User double-clicked");
		}  //  if (event.getClickCount() == 2)
	}  //  public void mousePressed()

	public void mouseReleased(MouseEvent event) {};
	public void mouseEntered(MouseEvent event) {};
	public void mouseExited(MouseEvent event) {};
	public void mouseClicked(MouseEvent event) {};

}  //  public class DoubleClickTest extends Panel implements MouseListener
Brett Spell
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date