Java : detect triple-click without firing double-click

前端 未结 7 959
旧巷少年郎
旧巷少年郎 2021-01-19 17:40

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

When the cell is triple-c

相关标签:
7条回答
  • 2021-01-19 18:24
      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());
              }
       }
    
    0 讨论(0)
  • 2021-01-19 18:31

    The previous answers are correct: you have to account for the timing and delay recognizing it as a double click until a certain amount of time has passed. The challenge is that, as you have noticed, the user could have a very long or very short double click threshold. So you need to know what the user's setting is. This other Stack Overflow thread ( Distinguish between a single click and a double click in Java ) mentions the awt.multiClickInterval desktop property. Try using that for your threshold.

    0 讨论(0)
  • 2021-01-19 18:34

    Here is what i have done to achieve this, this actually worked fine for me. A delay is necessary to detect the type of click. You can choose it. The following delays if a triple click can be happened within 400ms. You can decrease it to the extent till a consecutive click is not possible. If you are only worrying about the delay, then this is a highly negligible delay which must be essential to carry this out.

    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 讨论(0)
  • 2021-01-19 18:38

    It's exactly the same problem as detecting double-click without firing single click. You have to delay firing an event until you're sure there isn't a following click.

    0 讨论(0)
  • 2021-01-19 18:39

    You can do something like that, varying 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);
    }
    }
    
    0 讨论(0)
  • 2021-01-19 18:41

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

    Hint.

    if getClickCount()==2 then put it to wait.. for say like 200ms?

    0 讨论(0)
提交回复
热议问题