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.
We can implement a MouseListener interface when the mouse is stable while handling the mouse event whereas we can implement a MouseMotionListener interface when the mouse is in motion while handling the mouse event.
Interface MouseListener (To track mouse moves and mouse drags, use the MouseMotionListener .) The class that is interested in processing a mouse event either implements this interface (and all the methods it contains) or extends the abstract MouseAdapter class (overriding only the methods of interest).
MouseListener is an Interface and MouseAdapter is an implementation of that. You can use the MouseAdapter in every place that you use a MouseListener. But implementations have details that have be take in count.
Try using mousePressed instead of mouseClicked. mouseClicked looks for multiple button clicks, so it will coalesce some events.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With