tags:

views:

61

answers:

2

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.

+5  A: 

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

Ricky Clarkson
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
A: 

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.

aperkins
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?
DJClayworth
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: http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
camickr
@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
@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
@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
@aperkins: I have voted your answer down, as I believe the second paragraph is unclear. The host OS may adapt the reported click count based on user preference, but events are reported promptly. If I am mistaken, I'd welcome a counter-example. In either case, I can't undo the vote unless the answer is edited.
trashgod
Apologies to all if I was misleading - my memory of the event structure was slightly different than reality it seems, and I have not needed to tell the difference between double or single clicks in my recent work. I have edited the answer appropriately.
aperkins