Java : ignore single click on double click?

后端 未结 4 1331
名媛妹妹
名媛妹妹 2020-12-29 09:00

can anyone think of a good way to ignore the single click that comes with a double-click in Java ?

I\'m looking to have different behaviors for each such that:

4条回答
  •  时光说笑
    2020-12-29 09:31

    An alternative solution:

    I figured out this before I found the solution in this question. The idea is the same, use a timer, although more complicated :).

    Use SwingWorker:

    class BackgroundClickHandler extends SwingWorker {
      @Override
      protected Integer doInBackground() throws Exception {
        Thread.sleep(200);
        // Do what you want with single click
        return 0;
      }
    
    }
    

    In mouseClicked() method you can do something like this:

     if (event.getClickCount() == 1) {
      // We create a new instance everytime since
      // the execute() method is designed to be executed once
      clickHandler = new BackgroundClickHandler();
    
      try {
        clickHandler.execute();
      } catch(Exception e) {
        writer.println("Here!");
      }
    }
    
    if (event.getClickCount() == 2) {
      clickHandler.cancel(true);
    
      //Do what you want with double click
    }
    

    I hope it be useful.

提交回复
热议问题