user975705
user975705

Reputation: 331

Two simultaneous mouse listeners disrupting each other in Swing

My question concerns how to handle with simultaneous mouse events with Swing.

Both a MouseMotionListener and a MouseListener are added to follow a JPanel.

I have an object whose rotation takes places according to the x and y coordinates of the mouse pointers location (implemented by mouseMoved method of MouseMotionListener).

I've also got a MouseListener that performs its actions according to implemented mouseReleased() method.

The object is rotating neatly according to MouseMotionListener but a problem arises when the mouse button is pressed. As the mouse is now moved the rotation stops as obviously the JPanel is now waiting the mouseReleased() method to be executed.

Any ideas what's the best way to make these events happen simultaneously so that both the rotation according to the mouse pointers coordinates and operations according to mouseReleased() can be performed concurrently without disrupting each other?

Upvotes: 2

Views: 2545

Answers (2)

Rafe
Rafe

Reputation: 558

An old article going into swing threading specifics:

http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html?page=1

Worth a read as to the whys and hows.

Upvotes: 2

Adi Mor
Adi Mor

Reputation: 2165

If you wanted to do two action in your application simultaneously, you will do it in diffrent threads When you need to do something in diffrent threads in swing - you should use SwingWorker

Upvotes: 2

Related Questions