Max Roncace
Max Roncace

Reputation: 1262

MouseEvents in JFrame seem extremely unpredictable

I'm trying to code a level creator for my game in which you drag objects to their desired positions, but I'm having trouble figuring out whether or not the mouse button is being held down.

I'm using a MouseAdapter to listen for mouseClicked and mouseReleased events, but they seem to fire unpredictably. Usually, the program doesn't register either the mouse being clicked or released, but on occasion, one will be fired when it shouldn't be. A SOE will be thrown here and there, and eventually, they will be thrown repeatedly until the program is terminated. Any suggestions on working around this?

Upvotes: 1

Views: 53

Answers (1)

Aurand
Aurand

Reputation: 5537

I think you're probably listening for the wrong events. MOUSE_CLICKED means MOUSE_PRESSED + MOUSE_RELEASED.

I think you probably want to be looking for MOUSE_PRESSED instead of clicked.

See the api for MouseEvent for more details: http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html

Also, the MOUSE_DRAGGED event may be of some use to you.

Upvotes: 4

Related Questions