Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

identifying double click in java

People also ask

How do you know double-click?

To detect double clicks with JavaScript you can use the event listener dblclick . The dblclick event is supported in all modern browsers for desktop/laptops, even Internet Explorer 11.

What is Listselectionlistener in Java?

The List Selection Listener is basically invoked when the selection in the list has changed. These events are generally fired from the objects that implement the ListSelectionModel interface. Methods inside List Selection Listener. valueChaanged(ListSelectionEvent)

What is a double-click type?

Techopedia Explains Double Click A double click actually allows two actions to be completed with the help of same mouse button, similar to the Shift key on a keyboard. The functionalities of a double click differ based on the scenario in which it is used.

What is the difference between click and double-click?

Typically, a single click initiates a user interface action and a double-click extends the action. For example, one click usually selects an item, and a double-click edits the selected item.


public void mouseClicked(MouseEvent event)
{
  if (event.getClickCount() == 2 && event.getButton() == MouseEvent.BUTTON1) {
    System.out.println("double clicked");
  }
}

Assuming you mean in Swing, assign a MouseListener to your Component:

addMouseListener(new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent e){
        if(e.getClickCount()==2){
            // your code here
        }
    }
});

Reference:


The e.getClickCount()==2 is not enough if you want to allow your users to do multiple double clicks in a short delay. You are limited by the desktop configuration. You can get it by looking the result of Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

A good way to bypass the problem is not to use the getClickCount() check but to use a Timer where you can choose the interval max between your clicks and to handle by oneself the count (very simple).

The code associated :

boolean isAlreadyOneClick;

@Override
public void mouseClicked(MouseEvent mouseEvent) {
    if (isAlreadyOneClick) {
        System.out.println("double click");
        isAlreadyOneClick = false;
    } else {
        isAlreadyOneClick = true;
        Timer t = new Timer("doubleclickTimer", false);
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                isAlreadyOneClick = false;
            }
        }, 500);
    }
}

Tested with Win Xp OS and perfect.