7

I have a Class extending JFrame that is watching for a mouse click anywhere:

addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e){
        System.out.println("mouse was clicked");
    }
});

I usually have to wait nearly a second between clicks to trigger the event. If I make 2 or 3 clicks in a second, only one event fires. How do you watch for fast click events?

This is my first time using Java and I'm using NetBeans.

1
  • I see no latency; something else is slowing it down.
    – trashgod
    Sep 3, 2010 at 19:41

3 Answers 3

18

Try using mousePressed instead of mouseClicked. mouseClicked looks for multiple button clicks, so it will coalesce some events.

2
  • Just to clarify, the MouseEvent received by mouseClicked() reports the click count supplied by the host's native driver. The latter may or may not take user preferences into account, as discussed in comments following @aperkins' answer.
    – trashgod
    Sep 4, 2010 at 14:50
  • You are my hero. Apr 12, 2020 at 15:17
2

Hopefully this helps 3.5 years later for anyone searching for answers to the same problem :)

When you click on the mouse you will fire the following events.

  1. MousePressed
  2. MouseDragged (if you pressed hard enough to move the cursor slightly)
  3. MouseReleased
  4. MouseClicked

I ran into this very problem making the events the lazy way in Netbeans using their Forms utility. I found the accidental dragging of my mouse between Press and Release is what killed the click event. Working as intended or a minor failing of the JVM and Netbeans? I don't know.


The work-around I used was to register a MousePressed and MouseReleased event to simulate the clicking. If the Press and Release do not happen on the same Object, MouseReleased will do nothing.

If the Press and Release happen on the same Object, I call my method with appropriate parameters to consume the event.

Note that since I'm handling clicks on the JFrame, so it IS the only swing object, so I'm passing a Point object of the mouse coords and comparing both, ensuring they fall within the specified rectangle.

1

To expand a little on what @Ricky Clarkson said: MousePressed will fire every time a mouse button is pressed; MouseReleased will fire every time a mouse button is released, and MouseClicked events will fire every time the OS feels that the user is done clicking (i.e. they have clicked enough to overflow the click count or there was enough time from their last click for it to count as the finished click). the user presses and releases the mouse event.

If you want information on a mouse press, then use the MousePressed event. Otherwise, you will get the event of a MouseClicked whenever the OS wants to give it to Java, which can depend greatly on the settings of the system (i.e. how long of a delay is set in the System options - like the Control Panel - to allow for double clicks).

Hope this helps clarify.


Edit: Removed my statements related to the OS information - it seems I was mistaken in my recollection of how this worked. My apologies.

7
  • 1
    I thought that if you 'double-clicked' you would get two mouse-clicked events, one with a click-count of 1 and one with a click-count of 2? Sep 3, 2010 at 20:41
  • Really think this answer should be down voted, but I can only test on windows. The system settings have nothing to do with how many mouseClicked events are generated. A mouseClicked event is generated for every pressed/released event at the same mouse coordinate. The system settings only control the "click count". If you click slowly the count will always be 1. If you click fast (within the system settings) the click count keeps increasing. Try the tutorial demo: download.oracle.com/javase/tutorial/uiswing/events/…
    – camickr
    Sep 3, 2010 at 21:28
  • @camickr: Using MouseEventDemo on Mac OS X, I see a distinct change in the behavior of getClickCount() when I adjust the host's double-click speed; but I see no delay in reporting events, as suggested by @aperkins. I am unable to explain how the accepted answer impacts the latency asserted in the question.
    – trashgod
    Sep 4, 2010 at 0:54
  • @trashgod, what is the distinct change? I always get 3 events in the same order (pressed/released/clicked). The difference is the click count. When I click slowly (ie. every second) the click count is always 1. When I click more rapidly the click count keeps increasing (1, 2, 3, 4, 5, 6...). There are not lost events and the events are generated immediately.
    – camickr
    Sep 4, 2010 at 6:18
  • 1
    @camickr: I get your result on Mac OS and Windows. In addition, adjusting the host's double-click speed changes the interval between which repeated clicks serve to increment the result of getClickCount() versus returning to a count of one. For example, a long double-click interval allows me to see an incrementing getClickCount() by clicking at a leisurely pace (as you observe), while a short interval requires rapid clicking to see anything but one. On Ubuntu, the host preference appears to be ignored; but, like Mac OS and Windows, events are reported promptly.
    – trashgod
    Sep 4, 2010 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.