Java : detect triple-click without firing double-click

前端 未结 7 1021
旧巷少年郎
旧巷少年郎 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条回答
  •  Happy的楠姐
    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){}
                        }
    }
    

提交回复
热议问题