Java: triple-click detection without double-clicking

I have a JTable in which I want to call a function when a cell is double clicked and calls another function when the cell has a triple click.

When a triple-click cell, I do not want to call the double-click function.

Now I have (mgrdAlarm - JTable):

mgrdAlarm.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println("getClickCount() = " + e.getClickCount()); if (e.getClickCount()==2) { doubleClick(); System.out.println("Completed : doubleClick()"); } if (e.getClickCount()==3) { tripleClick(); System.out.println("Completed : tripleClick()"); } } }); 

Double-clicking the console displays:

 getClickCount() = 1 getClickCount() = 2 Completed : doubleClick() 

When you click on the console three times, it displays:

 getClickCount() = 1 getClickCount() = 2 Completed : doubleClick() getClickCount() = 3 Completed : tripleClick() 

With a triple click, I want to show the console:

 getClickCount() = 1 getClickCount() = 2 getClickCount() = 3 Completed : tripleClick() 

Therefore, I do not want to call the doubleClick () function when the cell has a triple click, but I want to call the doubleClick () function when the cell is double-clicked.

[EDIT]

Like all answers, the solution seems to be to delay the double-click action and wait a certain time for the triple-click.

But as discussed here , which can lead to a different type of problem: Perhaps the user set the double-click time long enough that it could overlap with the timeout of my triple-click.

It is not a real disaster if my double-click action is executed before my triple-click action, but it creates some additional overhead and especially some additional data traffic that I would like to prevent.

As the only solution so far may lead to other problems that can be really worse than the original problem, I will leave it as it is now.

+4
source share
7 answers

The previous answers are correct: you need to consider the time and delay, recognizing them as a double click, until a certain amount of time has passed. The challenge is that, as you have noticed, the user can have a very long or very short double-click threshold. Therefore, you need to know what user preference is. This other thread ( Distinguishes one click and double click in Java ) mentions the desktop property awt.multiClickInterval . Try using this for your doorstep.

+2
source
  public class TestMouseListener implements MouseListener { private boolean leftClick; private int clickCount; private boolean doubleClick; private boolean tripleClick; public void mouseClicked(MouseEvent evt) { if (evt.getButton()==MouseEvent.BUTTON1){ leftClick = true; clickCount = 0; if(evt.getClickCount() == 2) doubleClick=true; if(evt.getClickCount() == 3){ doubleClick = false; tripleClick = true; } Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); Timer timer = new Timer(timerinterval, new ActionListener() { public void actionPerformed(ActionEvent evt) { if(doubleClick){ System.out.println("double click."); clickCount++; if(clickCount == 2){ doubleClick(); //your doubleClick method clickCount=0; doubleClick = false; leftClick = false; } }else if (tripleClick) { System.out.println("Triple Click."); clickCount++; if(clickCount == 3) { tripleClick(); //your tripleClick method clickCount=0; tripleClick = false; leftClick = false; } } else if(leftClick) { System.out.println("single click."); leftClick = false; } } }); timer.setRepeats(false); timer.start(); if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop(); } } public static void main(String[] argv) throws Exception { JTextField component = new JTextField(); component.addMouseListener(new TestMouseListener()); JFrame f = new JFrame(); f.add(component); f.setSize(300, 300); f.setVisible(true); component.addMouseListener(new TestMouseListener()); } } 
+4
source

You can do something like this by changing the delay time:

 public class ClickForm extends JFrame { final static long CLICK_FREQUENTY = 300; static class ClickProcessor implements Runnable { Callable<Void> eventProcessor; ClickProcessor(Callable<Void> eventProcessor) { this.eventProcessor = eventProcessor; } @Override public void run() { try { Thread.sleep(CLICK_FREQUENTY); eventProcessor.call(); } catch (InterruptedException e) { // do nothing } catch (Exception e) { // do logging } } } public static void main(String[] args) { ClickForm f = new ClickForm(); f.setSize(400, 300); f.addMouseListener(new MouseAdapter() { Thread cp = null; public void mouseClicked(MouseEvent e) { System.out.println("getClickCount() = " + e.getClickCount() + ", e: " + e.toString()); if (cp != null && cp.isAlive()) cp.interrupt(); if (e.getClickCount() == 2) { cp = new Thread(new ClickProcessor(new Callable<Void>() { @Override public Void call() throws Exception { System.out.println("Double click processed"); return null; } })); cp.start(); } if (e.getClickCount() == 3) { cp = new Thread(new ClickProcessor(new Callable<Void>() { @Override public Void call() throws Exception { System.out.println("Triple click processed"); return null; } })); cp.start(); } } }); f.setVisible(true); } } 
+1
source

There is a tutorial for this here

Edit: it fires click events individually, so you get: One click THEN Double click THEN Triple Click. So you still have to make some secrets.

Code:

 import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JTextField; public class Main { public static void main(String[] argv) throws Exception { JTextField component = new JTextField(); component.addMouseListener(new MyMouseListener()); JFrame f = new JFrame(); f.add(component); f.setSize(300, 300); f.setVisible(true); component.addMouseListener(new MyMouseListener()); } } class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent evt) { if (evt.getClickCount() == 3) { System.out.println("triple-click"); } else if (evt.getClickCount() == 2) { System.out.println("double-click"); } } } 
+1
source

You need to delay the execution of double click to check if it has a tripple click .

Hint.

if getClickCount()==2 , then put it on 200ms .. for example, 200ms ?

0
source

This is exactly the same problem as double-click detection, without a single click. You must delay the triggering of the event until you are sure that there is no next click.

0
source

Here is what I did to achieve this, it really worked for me. A delay is required to detect the type of click. You can choose it. The following delays if a triple click can occur in 400 ms. You can reduce it until a consistent click is possible. If you are worried only about the delay, then this is a very slight delay, which must be significant for its implementation.

Here flag and t1 are global variables.

 public void mouseClicked(MouseEvent e) { int count=e.getClickCount(); if(count==3) { flag=true; System.out.println("Triple click"); } else if(count==2) { try { t1=new Timer(1,new ActionListener(){ public void actionPerformed(ActionEvent ae) { if(!flag) System.out.println("Double click"); flag=false; t1.stop(); } }); t1.setInitialDelay(400); t1.start(); }catch(Exception ex){} } } 
0
source

All Articles