File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Mouse events not getting captured: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mouse events not getting captured:" Watch "Mouse events not getting captured:" New topic

Mouse events not getting captured:

B. Smith

Joined: Oct 03, 2011
Posts: 10
First off, you guys are awesome. You have helped me before, and I appreciate it greatly.

This might actually be more appropriate for the threading and synchronization forum, but I thought I'd try this forum first, because I suspect my problem is in how I handle my canvas object:

I'm trying to make a fairly graphics intensive program (to be more blunt, a game) and I'm running into a problem with user input.

For some reason, my mouse events are not getting captured in my Swing JFrame. I'm trying to build a layer based application, where the mouse events captured by the frame get passed on up the line to objects that are supposed to handle them, but I think I'm lacking the conceptual understanding on how a mouseListener works, because it doesn't seem to be picked up by the frame itself.

Included is a SSCE. Run it, and click on the screen. It is intended that the console will print out at three lines per click, but it does not. In fact, in what is probably a related issue, the "X" button in the corner doesn't work either!

I've notice that when I comment out all the graphics code, my program (and SSCE) still fail to pick up mouse events. However, if I comment out all the graphics code as well as the thread.sleep call, then it works fine, up to and including the correct functionality of the "X" button. However, taking the thread.sleep out of the loop probably isn't a viable option, as my non-SSCE program is fairly graphics intensive and needs to be redrawn frequently, making a loop like this necessary.

In the SSCE, please forgive the abundance of code that doesn't really apply to the mouseListener problems. Lots of code is used to set the frame up, and another fair bit is used to set up the example "controller" layer that handles graphics and accepts mouseclicks. Please also forgive the cut-and-paste graphics code. I know how to handle a Graphics2D object once I have one, but figuring out how to get the right one to draw to is still somewhat of a dark art for me...

How can I make sure that my mouse events are getting captured, even while in a loop?

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
comment out the
//while (true)

now see if the frame closes (might get you started in the right direction)

also, mouseClicked() is generally a poor choice - it only fires if the x,y
of mousePressed and mouseReleased is identical
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

In other words, please read Concurrency in Swing.

How To Ask Questions How To Answer Questions
B. Smith

Joined: Oct 03, 2011
Posts: 10
Props to Rob Spoor for giving me the correct reading material and letting me solve my own problem!

It turns out that I was running my graphics processing in the same thread that ran my event dispatching thread. In order to make my program work, I changed my main method to what follows:

That makes it work wonderfully. Thanks for hooking me up with that article.

If this approach raises warning bells to anybody, let me know. If nobody warns me away from this course of action, I'll mark this as resolved tomorrow.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

what is a SSCELayer? i cant find it in the jdk7 API.

Visit my download page
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10824

Randall Twede wrote:what is a SSCELayer? i cant find it in the jdk7 API.

That's a custom class.
Check line#80 in the code posted above

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

B. Smith wrote:That makes it work wonderfully.

But not correctly. You are still accessing a GUI component (the canvas, although indirectly) from a non-EDT thread. Any access to GUI components, both querying and modifying, should be done from the EDT.
Why don't you use a javax.swing.Timer instead? The ActionListener's actionPerformed method will be executed on the EDT, and it won't cause any threading issues.
I agree. Here's the link:
subject: Mouse events not getting captured:
It's not a secret anymore!