Java : detect triple-click without firing double-click

前端 未结 7 958
旧巷少年郎
旧巷少年郎 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:44

    There's a tutorial for this here

    Edit: It fires click events individually though, so you would get: Single Click THEN Double Click THEN Triple Click. So you would still have to do some timing trickery.

    The code is:

    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");
        }
      }
    }
    

提交回复
热议问题