1

I have a java swing GUI in which at has to perform specific actions on double and single click. I have this code

 if (e.getClickCount() == 1)
    {
    // do something on single click
    }
    if (e.getClickCount() == 2)
    {
    // do something on double click
    }

its working correctly. problem is that this code behaves same for right and left mouse click. i do not want to perform any action on double right click. only want to show a popup menu on single right click.

Any body please guide me. thanks

Mikle Garin
  • 10,083
  • 37
  • 59
Asghar
  • 2,336
  • 8
  • 46
  • 79
  • *"right double click mouse event"* A 'right double click mouse event' on Windows 7 has the effect of opening a context menu (or similar) then ignoring the 2nd click. What is the use-case for altering that 'path of least surprise'? – Andrew Thompson Apr 16 '12 at 13:32

2 Answers2

3

You've got to check, in your MouseEvent object (variable "e"), which button caused the event :

if(e.getClickCount() == 2){ // two clicks, ok
    if((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK){
    // clicks are from BUTTON1, aka left click
    // double left click, insert code here      
    }
}
huelbois
  • 6,762
  • 1
  • 19
  • 21
1

you can use this code, where Double_Click is checked inside Swing Timer, another options is use Toolkit (never used) , maybe more info here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ClickListener extends MouseAdapter implements ActionListener {

    private final static int clickInterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
    private MouseEvent lastEvent;
    private Timer timer;

    public ClickListener() {
        this(clickInterval);
    }

    public ClickListener(int delay) {
        timer = new Timer(delay, this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        /*if (e.getClickCount() > 2) {
            return;
        }
        lastEvent = e;
        if (timer.isRunning()) {
            timer.stop();
            doubleClick(lastEvent);
        } else {
            timer.restart();
        }*/

        if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) {
            System.out.println("double");
            timer.stop();
        } else {
            timer.restart();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.stop();
        singleClick(lastEvent);
    }

    public void singleClick(MouseEvent e) {
    }

    public void doubleClick(MouseEvent e) {
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Double Click Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addMouseListener(new ClickListener() {

            @Override
            public void singleClick(MouseEvent e) {
                System.out.println("single");
            }

            @Override
            public void doubleClick(MouseEvent e) {
                System.out.println("double");
            }
        });
        frame.setPreferredSize(new Dimension(200, 200));
        frame.pack();
        frame.setVisible(true);
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319