Java : detect triple-click without firing double-click

前端 未结 7 963
旧巷少年郎
旧巷少年郎 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());
              }
       }
    

提交回复
热议问题