[java] MouseListener not always responding

Started by
5 comments, last by brewknowc 21 years, 11 months ago
Hi, We recently made a nice gui checkers game application in java and implemented a mouselistener from a jframe. Well when the user clicks the board sometimes it will handle the click, and sometimes it wont. The slower the system the less often it seems to handle the clicks. I was just wondering if anyone has come across this and knows how to fix it. Thanks - Free Your Mind -
- Free Your Mind -
Advertisement
I have also had this problem, and haven''t been able to find a complete solution.

One workaround I have found though, and this only works for left mouse button clicks, is, instead of using a panel, to use a JButton (assuming you are using Swing). Set the icon for the button to be your board, make some calls to change the appearance of the button when someone clicks on it, take away the border, it is possible to make it look nothing like a button. This way you can let the AWT\Swing code take care of listening on mouse clicks, and they always catch the click, and you''ll be notified through the ActionListener interface. Use the MouseMotionListener interface to keep track of where the mouse is so that you know where the click occured.

I''d be intersted to know if anyone else has found a workaround/solution for this though!

JJ.
--
www.javage.net
Just a thought, but could it be due to the ''click speed'' (how fast the user is pressing/releasing the mouse button)? Have you tried playing with mousePressed(MouseEvent e) and mouseReleased(MouseEvent e) on top of mouseClicked(MouseEvent e) ?

-pirate_dau
I''m using mousePressed and -released instead of mouseClicked. It is suitable for most applications and works much much better...
thanks for the suggestions, but does anyone know the reason this occurs? is it a problem with java itself? or is it something that i''m doing wrong? Thanks again


- Free Your Mind -
- Free Your Mind -
Well I''m not sure if this is similar to a problem I had. I was not using swing though and I was clicking from one window to another. I was making a map editor and the first window was the drawing screen, the second, I would choose my tiles from a list. I had to do a setfocus on the second window in order to do a 1-click to select the tile. Otherwise 1 click would go for the setfocus (even though I was clicking on the tile image itself) and another click to choose the tile.

It may be worth a shot.

Good luck,
Smoo
Just an idea I had about this,
have you tried enabling explicit event handling with this problem as opposed to using the more well-known listeners route? This would take your listening code one level ''up'' the mouse event processing hierarchy, it depends really on where the problem is as to whether this would work or not.

JJ.
--
www.javage.net

This topic is closed to new replies.

Advertisement