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 onthis 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()
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS